是否有可能找到给定包中的所有类或接口?(快速看了一下e.g. Package,似乎没有。)


当前回答

你需要查找类路径中的每个类装入器条目:

    String pkg = "org/apache/commons/lang";
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    URL[] urls = ((URLClassLoader) cl).getURLs();
    for (URL url : urls) {
        System.out.println(url.getFile());
        File jar = new File(url.getFile());
        // ....
    }   

如果条目是目录,只需在右边的子目录中查找:

if (jar.isDirectory()) {
    File subdir = new File(jar, pkg);
    if (!subdir.exists())
        continue;
    File[] files = subdir.listFiles();
    for (File file : files) {
        if (!file.isFile())
            continue;
        if (file.getName().endsWith(".class"))
            System.out.println("Found class: "
                    + file.getName().substring(0,
                            file.getName().length() - 6));
    }
}   

如果条目是文件,并且它是jar,检查它的ZIP条目:

else {
    // try to open as ZIP
    try {
        ZipFile zip = new ZipFile(jar);
        for (Enumeration<? extends ZipEntry> entries = zip
                .entries(); entries.hasMoreElements();) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (!name.startsWith(pkg))
                continue;
            name = name.substring(pkg.length() + 1);
            if (name.indexOf('/') < 0 && name.endsWith(".class"))
                System.out.println("Found class: "
                        + name.substring(0, name.length() - 6));
        }
    } catch (ZipException e) {
        System.out.println("Not a ZIP: " + e.getMessage());
    } catch (IOException e) {
        System.err.println(e.getMessage());
    }
}

现在,一旦包中有了所有的类名,就可以尝试用反射加载它们,并分析它们是类还是接口等等。

其他回答

不使用任何额外的库:

package test;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception{
        List<Class> classes = getClasses(Test.class.getClassLoader(),"test");
        for(Class c:classes){
            System.out.println("Class: "+c);
        }
    }

    public static List<Class> getClasses(ClassLoader cl,String pack) throws Exception{

        String dottedPackage = pack.replaceAll("[/]", ".");
        List<Class> classes = new ArrayList<Class>();
        URL upackage = cl.getResource(pack);

        DataInputStream dis = new DataInputStream((InputStream) upackage.getContent());
        String line = null;
        while ((line = dis.readLine()) != null) {
            if(line.endsWith(".class")) {
               classes.add(Class.forName(dottedPackage+"."+line.substring(0,line.lastIndexOf('.'))));
            }
        }
        return classes;
    }
}

目前列出给定包中所有类的最健壮的机制是ClassGraph,因为它处理尽可能广泛的类路径规范机制,包括新的JPMS模块系统。(我是作者。)

List<String> classNames = new ArrayList<>();
try (ScanResult scanResult = new ClassGraph().acceptPackages("my.package")
        .enableClassInfo().scan()) {
    classNames.addAll(scanResult.getAllClasses().getNames());
}

这将扫描类加载器和所有父加载器,以查找jar文件和目录。 jar文件和由jar的类路径引用的目录也会被加载。

this code is testet with Java 8,11,18. on 8 everything works perfectly using the URLClassLoader and the getURLs() method. on 11 it works fine using reflections, but the JVM prints a warning on the stderr stream (not redirectible with System.setErr() with my JVM) on 18 the reflections are useless (throws NoSuchMethod/Field), and the only thing (where I know that it works) is to use the getResource() method. When the class loader loades the resources of the given package from the file system a simple path url is returned. When the class loader loades the resources from a jar a url like 'jar:file:[jar-path]!/[in-jar-path]' is returned.

我使用了答案https://stackoverflow.com/a/1157352/18252455(来自一个重复的问题),并添加了读取类路径和搜索目录url的功能。

/**
 * orig description:<br>
 * Scans all classloaders for the current thread for loaded jars, and then scans
 * each jar for the package name in question, listing all classes directly under
 * the package name in question. Assumes directory structure in jar file and class
 * package naming follow java conventions (i.e. com.example.test.MyTest would be in
 * /com/example/test/MyTest.class)
 * <p>
 * in addition this method also scans for directories, where also is assumed, that the classes are
 * placed followed by the java conventions. (i.e. <code>com.example.test.MyTest</code> would be in
 * <code>directory/com/example/test/MyTest.class</code>)
 * <p>
 * this method also reads the jars Class-Path for other jars and directories. for the jars and
 * directories referred in the jars are scanned with the same rules as defined here.<br>
 * it is ensured that no jar/directory is scanned exactly one time.
 * <p>
 * if {@code bailError} is <code>true</code> all errors will be wrapped in a
 * {@link RuntimeException}
 * and then thrown.<br>
 * a {@link RuntimeException} will also be thrown if something unexpected happens.<br>
 * 
 * @param packageName
 *            the name of the package for which the classes should be searched
 * @param allowSubPackages
 *            <code>true</code> is also classes in sub packages should be found
 * @param loader
 *            the {@link ClassLoader} which should be used to find the URLs and to load classes
 * @param bailError
 *            if all {@link Exception} should be re-thrown wrapped in {@link RuntimeException} and
 *            if a {@link RuntimeException} should be thrown, when something is not as expected.
 * @see https://stackoverflow.com/questions/1156552/java-package-introspection
 * @see https://stackoverflow.com/a/1157352/18252455
 * @see https://creativecommons.org/licenses/by-sa/2.5/
 * @see https://creativecommons.org/licenses/by-sa/2.5/legalcode
 */
public static Set <Class <?>> tryGetClassesForPackage(String packageName, boolean allowSubPackages, ClassLoader loader, boolean bailError) {
    Set <URL> jarUrls = new HashSet <URL>();
    Set <Path> directorys = new HashSet <Path>();
    findClassPools(loader, jarUrls, directorys, bailError, packageName);
    Set <Class <?>> jarClasses = findJarClasses(allowSubPackages, packageName, jarUrls, directorys, loader, bailError);
    Set <Class <?>> dirClasses = findDirClasses(allowSubPackages, packageName, directorys, loader, bailError);
    jarClasses.addAll(dirClasses);
    return jarClasses;
}

private static Set <Class <?>> findDirClasses(boolean subPackages, String packageName, Set <Path> directorys, ClassLoader loader, boolean bailError) {
    Filter <Path> filter;
    Set <Class <?>> result = new HashSet <>();
    for (Path startPath : directorys) {
        String packagePath = packageName.replace(".", startPath.getFileSystem().getSeparator());
        final Path searchPath = startPath.resolve(packagePath).toAbsolutePath();
        if (subPackages) {
            filter = p -> {
                p = p.toAbsolutePath();
                Path other;
                if (p.getNameCount() >= searchPath.getNameCount()) {
                    other = searchPath;
                } else {
                    other = searchPath.subpath(0, p.getNameCount());
                }
                if (p.startsWith(other)) {
                    return true;
                } else {
                    return false;
                }
            };
        } else {
            filter = p -> {
                p = p.toAbsolutePath();
                if (p.getNameCount() > searchPath.getNameCount() + 1) {
                    return false;
                } else if (p.toAbsolutePath().startsWith(searchPath)) {
                    return true;
                } else {
                    return false;
                }
            };
        }
        if (Files.exists(searchPath)) {
            findDirClassFilesRecursive(filter, searchPath, startPath, result, loader, bailError);
        } // the package does not have to exist in every directory
    }
    return result;
}

private static void findDirClassFilesRecursive(Filter <Path> filter, Path path, Path start, Set <Class <?>> classes, ClassLoader loader, boolean bailError) {
    try (DirectoryStream <Path> dirStream = Files.newDirectoryStream(path, filter)) {
        for (Path p : dirStream) {
            if (Files.isDirectory(p)) {
                findDirClassFilesRecursive(filter, p, start, classes, loader, bailError);
            } else {
                Path subp = p.subpath(start.getNameCount(), p.getNameCount());
                String str = subp.toString();
                if (str.endsWith(".class")) {
                    str = str.substring(0, str.length() - 6);
                    String sep = p.getFileSystem().getSeparator();
                    if (str.startsWith(sep)) {
                        str = str.substring(sep.length());
                    }
                    if (str.endsWith(sep)) {
                        str = str.substring(0, str.length() - sep.length());
                    }
                    String fullClassName = str.replace(sep, ".");
                    try {
                        Class <?> cls = Class.forName(fullClassName, false, loader);
                        classes.add(cls);
                    } catch (ClassNotFoundException e) {
                        if (bailError) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }
    } catch (IOException e) {
        if (bailError) {
            throw new RuntimeException(e);
        }
    }
}

private static Set <Class <?>> findJarClasses(boolean subPackages, String packageName, Set <URL> nextJarUrls, Set <Path> directories, ClassLoader loader, boolean bailError) {
    String packagePath = packageName.replace('.', '/');
    Set <Class <?>> result = new HashSet <>();
    Set <URL> allJarUrls = new HashSet <>();
    while (true) {
        Set <URL> thisJarUrls = new HashSet <>(nextJarUrls);
        thisJarUrls.removeAll(allJarUrls);
        if (thisJarUrls.isEmpty()) {
            break;
        }
        allJarUrls.addAll(thisJarUrls);
        for (URL url : thisJarUrls) {
            try (JarInputStream stream = new JarInputStream(url.openStream())) {
                // may want better way to open url connections
                readJarClassPath(stream, nextJarUrls, directories, bailError);
                JarEntry entry = stream.getNextJarEntry();
                while (entry != null) {
                    String name = entry.getName();
                    int i = name.lastIndexOf("/");
                    
                    if (i > 0 && name.endsWith(".class")) {
                        try {
                            if (subPackages) {
                                if (name.substring(0, i).startsWith(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            } else {
                                if (name.substring(0, i).equals(packagePath)) {
                                    Class <?> cls = Class.forName(name.substring(0, name.length() - 6).replace("/", "."), false, loader);
                                    result.add(cls);
                                }
                            }
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                    entry = stream.getNextJarEntry();
                }
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

private static void readJarClassPath(JarInputStream stream, Set <URL> jarUrls, Set <Path> directories, boolean bailError) {
    Object classPathObj = stream.getManifest().getMainAttributes().get(new Name("Class-Path"));
    if (classPathObj == null) {
        return;
    }
    if (classPathObj instanceof String) {
        String[] entries = ((String) classPathObj).split("\\s+");// should also work with a single space (" ")
        for (String entry : entries) {
            try {
                URL url = new URL(entry);
                addFromUrl(jarUrls, directories, url, bailError);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    } else if (bailError) {
        throw new RuntimeException("the Class-Path attribute is no String: " + classPathObj.getClass().getName() + " tos='" + classPathObj + "'");
    }
}

private static void findClassPools(ClassLoader classLoader, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, String packageName) {
    packageName = packageName.replace('.', '/');
    while (classLoader != null) {
        if (classLoader instanceof URLClassLoader) {
            for (URL url : ((URLClassLoader) classLoader).getURLs()) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
                System.out.println("rurl-class-loade.url[n]r->'" + url + "'");
            }
        } else {
            URL res = classLoader.getResource("");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/");
            if (res != null) {
                addFromUrl(jarUrls, directoryPaths, res, bailError);
            }
            res = classLoader.getResource("/" + packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            res = classLoader.getResource(packageName);
            if (res != null) {
                res = removePackageFromUrl(res, packageName, bailError);
                if (res != null) {
                    addFromUrl(jarUrls, directoryPaths, res, bailError);
                }
            }
            addFromUnknownClass(classLoader, jarUrls, directoryPaths, bailError, 8);
        }
        classLoader = classLoader.getParent();
    }
}

private static URL removePackageFromUrl(URL res, String packagePath, boolean bailError) {
    packagePath = "/" + packagePath;
    String urlStr = res.toString();
    if ( !urlStr.endsWith(packagePath)) {
        if (bailError) {
            throw new RuntimeException("the url string does not end with the packagepath! packagePath='" + packagePath + "' urlStr='" + urlStr + "'");
        } else {
            return null;
        }
    }
    urlStr = urlStr.substring(0, urlStr.length() - packagePath.length());
    if (urlStr.endsWith("!")) {
        urlStr = urlStr.substring(0, urlStr.length() - 1);
    }
    if (urlStr.startsWith("jar:")) {
        urlStr = urlStr.substring(4);
    }
    try {
        return new URL(urlStr);
    } catch (MalformedURLException e) {
        if (bailError) {
            throw new RuntimeException(e);
        } else {
            return null;
        }
    }
}

private static void addFromUnknownClass(Object instance, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, int maxDeep) {
    Class <?> cls = instance.getClass();
    while (cls != null) {
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            Class <?> type = field.getType();
            Object value;
            try {
                value = getValue(instance, field);
                if (value != null) {
                    addFromUnknownValue(value, jarUrls, directoryPaths, bailError, type, field.getName(), maxDeep - 1);
                }
            } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
                if (bailError) {
                    final String version = System.getProperty("java.version");
                    String vers = version;
                    if (vers.startsWith("1.")) {
                        vers = vers.substring(2);
                    }
                    int dotindex = vers.indexOf('.');
                    if (dotindex != -1) {
                        vers = vers.substring(0, dotindex);
                    }
                    int versNum;
                    try {
                        versNum = Integer.parseInt(vers);
                    } catch (NumberFormatException e1) {
                        throw new RuntimeException("illegal version: '" + version + "' lastError: " + e.getMessage(), e);
                    }
                    if (versNum <= 11) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        cls = cls.getSuperclass();
    }
    
}

private static Object getValue(Object instance, Field field) throws IllegalArgumentException, IllegalAccessException, SecurityException {
    try {
        boolean flag = field.isAccessible();
        boolean newflag = flag;
        try {
            field.setAccessible(true);
            newflag = true;
        } catch (Exception e) {}
        try {
            return field.get(instance);
        } finally {
            if (flag != newflag) {
                field.setAccessible(flag);
            }
        }
    } catch (IllegalArgumentException | IllegalAccessException | SecurityException e) {
        try {
            Field override = AccessibleObject.class.getDeclaredField("override");
            boolean flag = override.isAccessible();
            boolean newFlag = flag;
            try {
                override.setAccessible(true);
                flag = true;
            } catch (Exception s) {}
            override.setBoolean(field, true);
            if (flag != newFlag) {
                override.setAccessible(flag);
            }
            return field.get(instance);
        } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e1) {
            e.addSuppressed(e1);
            throw e;
        }
    }
}

private static void addFromUnknownValue(Object value, Set <URL> jarUrls, Set <Path> directoryPaths, boolean bailError, Class <?> type, String fieldName, int maxDeep) {
    if (Collection.class.isAssignableFrom(type)) {
        for (Object obj : (Collection <?>) value) {
            URL url = null;
            try {
                if (obj instanceof URL) {
                    url = (URL) obj;
                } else if (obj instanceof Path) {
                    url = ((Path) obj).toUri().toURL();
                } else if (obj instanceof File) {
                    url = ((File) obj).toURI().toURL();
                }
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
            if (url != null) {
                addFromUrl(jarUrls, directoryPaths, url, bailError);
            }
        }
    } else if (URL[].class.isAssignableFrom(type)) {
        for (URL url : (URL[]) value) {
            addFromUrl(jarUrls, directoryPaths, url, bailError);
        }
    } else if (Path[].class.isAssignableFrom(type)) {
        for (Path path : (Path[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, path.toUri().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (File[].class.isAssignableFrom(type)) {
        for (File file : (File[]) value) {
            try {
                addFromUrl(jarUrls, directoryPaths, file.toURI().toURL(), bailError);
            } catch (MalformedURLException e) {
                if (bailError) {
                    throw new RuntimeException(e);
                }
            }
        }
    } else if (maxDeep > 0) {
        addFromUnknownClass(value, jarUrls, directoryPaths, bailError, maxDeep - 1);
    }
}

private static void addFromUrl(Set <URL> jarUrls, Set <Path> directoryPaths, URL url, boolean bailError) {
    if (url.getFile().endsWith(".jar") || url.getFile().endsWith(".zip")) {
        // may want better way to detect jar files
        jarUrls.add(url);
    } else {
        try {
            Path path = Paths.get(url.toURI());
            if (Files.isDirectory(path)) {
                directoryPaths.add(path);
            } else if (bailError) {
                throw new RuntimeException("unknown url for class loading: " + url);
            }
        } catch (URISyntaxException e) {
            if (bailError) {
                throw new RuntimeException(e);
            }
        }
    }
}

进口:

import java.io.File;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.DirectoryStream;
import java.nio.file.DirectoryStream.Filter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Attributes.Name;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

几乎所有的答案要么使用reflection,要么从文件系统读取类文件。如果尝试从文件系统读取类,则在将应用程序打包为JAR或其他格式时可能会出错。此外,您可能不想为此目的使用单独的库。

这里有另一种方法,它是纯java,不依赖于文件系统。

import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class PackageUtil {

    public static Collection<Class> getClasses(final String pack) throws Exception {
        final StandardJavaFileManager fileManager = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
        return StreamSupport.stream(fileManager.list(StandardLocation.CLASS_PATH, pack, Collections.singleton(JavaFileObject.Kind.CLASS), false).spliterator(), false)
                .map(javaFileObject -> {
                    try {
                        final String[] split = javaFileObject.getName()
                                .replace(".class", "")
                                .replace(")", "")
                                .split(Pattern.quote(File.separator));

                        final String fullClassName = pack + "." + split[split.length - 1];
                        return Class.forName(fullClassName);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }

                })
                .collect(Collectors.toCollection(ArrayList::new));
    }
}

Java 8不是必须的。你可以使用for循环代替流。 你可以这样测试

public static void main(String[] args) throws Exception {
    final String pack = "java.nio.file"; // Or any other package
    PackageUtil.getClasses(pack).stream().forEach(System.out::println);
}

如果您不使用任何动态类加载器,您可以搜索类路径,并为每个条目搜索目录或JAR文件。