Applet 和动态类加载问题
首先,这是我第一次问 java 问题,所以我希望我能让自己理解:-) 提前感谢任何研究这个问题的人。
我有一个小程序,我需要根据用户与小程序的交互来加载类。我已经签署了该小程序,因此安全性不应该成为问题(如果我理解正确的话)。我的所有文件都位于 applet.jar 中,其中包含一个名为 p6applet 的包,所有 .class 文件都位于该包内。 我使用以下代码来测试小程序:
<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
我的小程序在 netbeans 小程序查看器中运行良好,如果这可以帮助查明问题。我怀疑问题是需要加载的文件无法成功导入接口AlgorithmExecutable。任何帮助将不胜感激。
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.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为主要问题是您没有使用 file:// 的安全权限,即使小程序已签名并给出了安全权限也是如此。据我所知,您需要访问本地服务器上的 URL,或者类路径上的 jar 内的 URL。这是一个复杂的问题。
http://www.securingjava.com/chapter-two/chapter-二-7.html
http://forums.sun .com/thread.jspa?threadID=5427882&tstart=0
http://mindprod. com/jgloss/applet.html
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