apchester
apchester

Reputation: 1144

Dynamic loading problem when program executes from a jar

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

Upvotes: 1

Views: 190

Answers (2)

apchester
apchester

Reputation: 1144

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

Upvotes: 1

Rob Spieldenner
Rob Spieldenner

Reputation: 1698

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?

Upvotes: 0

Related Questions