mardi 28 novembre 2017

ClassNotFoundException when loading classes from a jar file with reflection

I load classes at runtime from a .jar file using the following code:

try
{
    // Get all the files in mod folder
    File[] mods = new File("mod").listFiles();

    for (int i=0; i<mods.length; i++)
    {
        // Skip if the file is not a jar
        if (!mods[i].getName().endsWith(".jar"))
            continue;

        // Create a JarFile
        JarFile jarFile = new JarFile(mods[i]);

        // Get the entries
        Enumeration e = jarFile.entries();

        // Create a URL for the jar
        URL[] urls = { new URL("jar:file:" + mods[i].getAbsolutePath() +"!/") };
        cl = URLClassLoader.newInstance(urls);

        while (e.hasMoreElements())
        {
            JarEntry je = (JarEntry) e.nextElement();

            // Skip directories
            if(je.isDirectory() || !je.getName().endsWith(".class"))
            {
                continue;
            }

            // -6 because of .class
            String className = je.getName().substring(0,je.getName().length()-6);
            className = className.replace('/', '.');

            // Load the class
            Class c = cl.loadClass(className);
        }
    }
}
catch (Exception e)
{
    e.printStackTrace();
}

After loading the classes, I need the files available via reflection:

getClassLoader().loadClass(packageName + "." + className.replace('.', '$'))

However, this second line is done by a library which I cannot change. So I need to adapt my code (the upper code block) to load the classes in a way it would be available to the classloader of the external library I'm using (JavaFX, by a reference in a fxml file). How can I do this?





Aucun commentaire:

Enregistrer un commentaire