First of all this is the first time I am asking a java question so I hope I make myself understandable :-) Thanks in advance for anyone looking into this.
I have an applet, where I need to load classes depending on the users interaction with the applet. I have signed the applet so security should not be an issue (if I understand this correctly). All my files are located in applet.jar, which contains a package called p6applet and inside the package all the .class files are located. I use the following code to test the applet:
<APPLET
CODE="p6applet.Main.class"
ARCHIVE = applet.jar
WIDTH=0
HEIGHT=0>
</APPLET>
The class that needs to be loaded is located on the users hard drive. It needs to import an interface, that is located within the applet.jar file under the package p6applet. This interface is called AlgorithmExecutable.
When I run my application it executes the follow code, where after an error happens: The error happens when the program executes the following lines of code:
className = ...;
try {
System.out.println("Test1");
URL[] urlClass = {new URL("file:/C:/.../")};
System.out.println("Test2");
ClassLoader cl = new URLClassLoader(urlClass);
System.out.println("Test3");
algorithmClass = cl.loadClass(className); //This is where the error occurs
System.out.println("Test4");
algorithm = (AlgorithmExecutable) algorithmClass.newInstance();
System.out.println("The " + className + " has been loaded.");
} catch (MalformedURLException ex) {
System.out.println("MalformedURLException: " + ex);
} catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException: " + ex);
} catch (InstantiationException ex) {
System.out.println("InstantiationException: " + ex);
} catch (IllegalAccessException ex) {
System.out.println("IllegalAccessException: " + ex);
}
I get the following error:
Exception in thread "AWT-EventQueue-7" java.lang.NoClassDefFoundError: p6applet/AlgorithmExecutable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at p6applet.AlgorithmHandler.getAlgorithm(AlgorithmHandler.java:52)
at p6applet.AlgorithmInterface.actionPerformed(AlgorithmInterface.java:671)
at javax.swing.AbstractButton.开发者_Python百科fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: p6applet.AlgorithmExecutable
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 38 more
My applet runs fine in the netbeans applet viewer if that can help pinpoint the problem. I suspect the problem is that the file that needs to be loaded can't succesfully import the interface AlgorithmExecutable. Any help would be appreciated.
I think the main problem is that you don't have security permissions to use file://, possibly even if the applet is signed and security permissions are given. You need to access URLs on the local server, or within jars on the classpath, as far as I know. It's a complex issue.
http://www.securingjava.com/chapter-two/chapter-two-7.html
http://forums.sun.com/thread.jspa?threadID=5427882&tstart=0
http://mindprod.com/jgloss/applet.html
精彩评论