1

The code block below is inside a function that accesses a jar file. I have already checked to see if the path to the file is correct. When printing the classnames from the jar file to the console, i see all the correct classnames. "model_with_HSQLDB_1_8.HSQLDB_Local_Model_1_8" is among one of those class names. When i try to load that class using the code below, i get an error.


Class myClass = getClassFromJarFile(jarfile, "model_with_HSQLDB_1_8.HSQLDB_Local_Model_1_8");


public static Class getClassFromJarFile(File jarfile, String className){

    try (URLClassLoader cl = URLClassLoader.newInstance(
                 new URL[] { new URL("jar:file:" + jarFile + "!/") })) { 

        return cl.loadClass(className);
    }
}

The cl.loadClass() throws the Error.

I get the following error message:

    Exception in thread "main" java.lang.NoClassDefFoundError: interfaces/Modelable_1_8...
    Caused by: java.lang.ClassNotFoundException: interfaces.Modelable_1_8

the weird thing is that the error is "java.lang.ClassNotFoundException: interfaces.Modelable_1_8" instead of something like "java.lang.ClassNotFoundException: model_with_HSQLDB_1_8.HSQLDB_Local_Model_1_8"

Any help on this topic would be great!

pbuort
  • 11
  • 1
  • 1
    Loading one class might require also loading other classes that it depends on. – Tim Moore Mar 09 '23 at 09:31
  • Is there a way that this can be done automatically, or do i have to do that manually from inside the main method? – pbuort Mar 09 '23 at 10:52
  • It’s automatic, but all of the dependencies need to be available to the class loader (or its parents). – Tim Moore Mar 09 '23 at 11:02
  • I checked the project and found that the model_with_HSQLDB class uses the Modelable_1_8 interface and that the problem is probably with that. It's also pretty old code (Java 1.8) that i was trying to run on Java 11 or newer. I've also seen a lot of posts that from Java 1.8 to the later versions quite a few breaking changes occurred. – pbuort Mar 27 '23 at 07:01

0 Answers0