PDA

View Full Version : Load Classes from external jar



ssudan
04-06-2006, 03:51 AM
Hi,

I want to load some classes from an external jar file. The code goes as

URL[] url = new URL[]{new URL("jar:file:d:\\Install\\hello.jar!/")};
URLClassLoader loader1 = new URLClassLoader(url);
Class cls1 = Class.forName("Hello",true,loader1);
Method meth[] = cls1.getMethods();
for(int i=0;i<meth.length;i++)
{

if(meth[i].getName().equals("add"))
{

meth[i].invoke(cls1.newInstance(),new Object[]{});
}
}

But this throws an ClassNotFoundException. The output is

java.lang.ClassNotFoundException: Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at com.installshield.test.event.dialog.swing.PanelDestination.enteredDes
tination(PanelDestination.java:263)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.installshield.event.ActionSequenceEngine$ActionTask.invokeJavaMet
hod(Unknown Source)
at com.installshield.event.ActionSequenceEngine$ActionTask.executeAction
(Unknown Source)
at com.installshield.event.ActionSequenceEngine$ActionTask.run(Unknown S
ource)
at com.installshield.event.ThreadPool.run(Unknown Source)
at java.lang.Thread.run(Thread.java:534)


Could anyone help me on resolving this.

Thanks in advance
Sudan

Eyal Goren
04-08-2006, 04:03 PM
Hi,

If you use a custom bean, you need to add in the build method a call for the putArchive, and give the jar as parameter (Assuming you need the jar that exist on your build environment).

Eyal