views:

139

answers:

4

Hi,

my app basically stores a jar file in a "Blob" in mysql and then grabs it through a PHP file. This is not a problem, because doing:

$ wget http://path/to/php
$ jar tf file.jar

shows the classes perfectly fine. I have tried many things, and the farthest I've gotten is when using ClassLoader.loadClass() when I call newInstance() it stops there; no exception is thrown (looking at the source it's not a runtime exception).

The thing is, it "loads" it fine, in the sense I can dump the file names, and view the methods contained within it (this class is NOT on the classpath). I cannot call methods; there is no constructor with args.

This is the basic method I'm currently trying (I've tried other variations):

URL[] urls = new URL[] { new URL("jar:http://path/to/file!/") };
URLClassLoader cl = new URLClassLoader(urls);

// getClassName(int) just looks at my webserver for what class name to use.
// the jar is composed of one main class with subclasses, ie
// Class
// Class$Sub
// Class$A

Class<?> s = cl.loadClass(getClassName(id)); // Class.forName() throws ClassNotFound
s.newInstance(); // crashes here
System.out.println("Done."); // to illustrate if it DID finish

// method.invoke() will just throw NPE, as well.

Any help is greatly appreciated. Could the subclasses actually be the problem?

Thank you.

+1  A: 

What do you mean be "crashes" and what is the full exception stack trace?

It looks like your problem is that you are not specifying the jar file in the URL.

URL[] urls = new URL[] { new URL("jar:http://path/to/file!/") };

Should become

URL[] urls = { new URL("http://path/to/file.jar") };
Tom Hawtin - tackline
Sorry for the late reply - I have used without jar: the first time I started this.No exception is visibly thrown - it just quits as if System.exit(0) was triggered. The method throws Exception so any exceptions are printed.
A: 

Try to remove the / at the end of the URL,

URL[] urls = new URL[] { new URL("jar:http://path/to/file!") };

IF the URL ends with "/", the loader will treat it as a directory. Otherwise, it will try to load it as a JAR.

ZZ Coder
A: 

Well I got it working - it was a design flaw in the app that was contained in the jar that was being loaded - my bad :)