简体   繁体   中英

Reflection doesn't work after packing

I get this code here on stack overflow. It's a good one but only works in Netbeans. After I generated the .jar file don't work anymore and don't give me error messages.

 /**
 * Scans all classes accessible from the context class loader which belong
 * to the given package and subpackages.
 *
 * @param packageName The base package
 * @return The classes
 * @throws ClassNotFoundException
 * @throws IOException
 */
public Class[] getClasses(String packageName)
        throws ClassNotFoundException, IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
}

/**
 * Recursive method used to find all classes in a given directory and
 * subdirs.
 *
 * @param directory The base directory
 * @param packageName The package name for classes found inside the base
 * directory
 * @return The classes
 * @throws ClassNotFoundException
 */
private List<Class> findClasses(File directory, String packageName) throws ClassNotFoundException {
    List<Class> classes = new ArrayList<Class>();
    if (!directory.exists()) {
        return classes;
    }
    File[] files = directory.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            assert !file.getName().contains(".");
            classes.addAll(findClasses(file, packageName + "." + file.getName()));
        } else if (file.getName().endsWith(".class")) {
            classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
        }
    }
    return classes;
}

This piece of code was encapsulated in another project to be used as my JMenu Library. but only works in netbeans. I don't understand why.

update

I will try to explain better.

I have a project to work with JMenu that works through reflection to read the classes in the other project. This project will be using as a library to others projects. When it's run on netbeans do well, but when a generate the .jar file doesn't work anymore and I receive no error messages.

When all the resources is packed in a jar there are no files anymore.

Instead use this.class.getResource() or this.class.getResourceAsStream()

Or you can use some libraries see Can you find all classes in a package using reflection?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM