开发者

Dynamic loading problem when program executes from a jar

开发者 https://www.devze.com 2022-12-08 21:35 出处:网络
I have an application which is extendable through java classes that conform to a given interface. If I run the program from the command line classes, I am able to instantiate the add-on classes using:

I have an application which is extendable through java classes that conform to a given interface. If I run the program from the command line classes, I am able to instantiate the add-on classes using:

Class.forName("myAddon").newInstance();

However if I jar the application (setting the main开发者_如何转开发 class correctly), I get a class not found exception. Can anybody shed some light on what's going on here?

TIA, Adam


I have managed to get this working using a URLClassLoader, specifying the search path to be the current directory as follows:

URLClassLoader cl = new URLClassLoader(new URL[] {new File(".").toURI().toURL()});

Thanks for all your help,

Adam


The class you're looking for isn't on the classpath. If its in a directory

i.e. MyClass.class MyJar.jar

You need to do:

java -classpath . -jar MyJar.jar

But really we need more info to answer the question. What is the main class set to in the jar manifest? What's your classpath? How do you specify which class to load?

0

精彩评论

暂无评论...
验证码 换一张
取 消