Here you can find the source of findClasses(String directory, String packageName)
Parameter | Description |
---|---|
directory | The base directory |
packageName | The package name for classes found inside the base directory |
Parameter | Description |
---|---|
ClassNotFoundException | an exception |
private static TreeSet<String> findClasses(String directory, String packageName) throws Exception
//package com.java2s; import java.io.File; import java.net.URL; import java.util.TreeSet; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { /**/* w w w. j a v a2 s . c o m*/ * Recursive method used to find all classes in a given directory and * subdirs. Adapted from http://snippets.dzone.com/posts/show/4831 and * extended to support use of JAR files * * @param directory * The base directory * @param packageName * The package name for classes found inside the base directory * @return The classes * @throws ClassNotFoundException */ private static TreeSet<String> findClasses(String directory, String packageName) throws Exception { TreeSet<String> classes = new TreeSet<String>(); if (directory.startsWith("file:") && directory.contains("!")) { String[] split = directory.split("!"); URL jar = new URL(split[0]); ZipInputStream zip = new ZipInputStream(jar.openStream()); ZipEntry entry = null; while ((entry = zip.getNextEntry()) != null) { if (entry.getName().endsWith(".class")) { String className = entry.getName().replaceAll("[$].*", "").replaceAll("[.]class", "") .replace('/', '.'); classes.add(className); } } } File dir = new File(directory); if (!dir.exists()) { return classes; } File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { assert !file.getName().contains("."); classes.addAll(findClasses(file.getAbsolutePath(), packageName + "." + file.getName())); } else if (file.getName().endsWith(".class")) { classes.add(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)); } } return classes; } }