Applet和动态类加载问题

时间:2010-05-24 14:34:58

标签: java applet

首先,这是我第一次提出一个java问题所以我希望自己可以理解:-)提前感谢所有关注此事的人。

我有一个applet,我需要根据用户与applet的交互来加载类。我已经签署了applet,因此安全性不应成为问题(如果我理解正确的话)。我的所有文件都位于applet.jar中,其中包含一个名为p6applet的包,并且包中包含所有.class文件。 我使用以下代码来测试applet:

<APPLET 
     CODE="p6applet.Main.class" 
     ARCHIVE = applet.jar 
         WIDTH=0 
         HEIGHT=0>
</APPLET>

需要加载的类位于用户硬盘上。它需要导入一个接口,该接口位于包p6applet下的applet.jar文件中。此接口称为AlgorithmExecutable。

当我运行我的应用程序时,它执行以下代码,在发生错误后: 程序执行以下代码行时会发生错误:

    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);
}

我收到以下错误:

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.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

如果可以帮助查明问题,我的applet在netbeans applet viewer中运行正常。我怀疑问题是需要加载的文件无法成功导入AlgorithmExecutable接口。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

我认为主要问题是你没有安全权限来使用file://,即使applet已经签名并且给出了安全权限。据我所知,您需要访问本地服务器上的URL或类路径中的jar。这是一个复杂的问题。

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