我的java applet对其嵌入的图像进行访问被拒绝

时间:2012-05-22 17:55:54

标签: java applet

我正在编写一个Java国际象棋gui。它作为Eclipse的applet运行良好,但是我很难从HTML页面在本地机器上运行它。我正在使用我在JAR中包含的图像。使用Eclipse Debug可以正确访问映像。我错过了什么:(?

HTML如下所示:

<html>
<applet code="Ginsie" 
        height="300px" width="300px" archive="chess.jar">
</applet>
</html>

Java控制台的输出(从浏览器打开):

java.lang.RuntimeException: java.lang.ExceptionInInitializerError
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ExceptionInInitializerError
    at BoardPanel.loadSquares(BoardPanel.java:105)
    at BoardPanel.<init>(BoardPanel.java:34)
    at Ginsie.<init>(Ginsie.java:60)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(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.security.AccessControlException: access denied (java.io.FilePermission ..\img\squares\Wood\BSQUARE.jpg read)
    at java.security.AccessControlContext.checkPermission(Unknown Source)
    at java.security.AccessController.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkPermission(Unknown Source)
    at java.lang.SecurityManager.checkRead(Unknown Source)
    at java.io.File.canRead(Unknown Source)
    at javax.imageio.ImageIO.read(Unknown Source)
    at Square.<clinit>(Square.java:47)
    ... 24 more
Exception : java.lang.RuntimeException: java.lang.ExceptionInInitializerError

1 个答案:

答案 0 :(得分:4)

从堆栈跟踪判断,看起来好像在调用javax.imageio.ImageIO.read(File)。在Eclipse中,它可能有效,因为它正在访问本地文件系统来读取图像。尝试使用Class.getResourceAsStream()从类路径(即从jar)读取文件,然后将InputStream传递给javax.imageio.ImageIO.read(InputStream)。