Java Applet中的文件权限错误

时间:2012-04-08 02:26:02

标签: java file permissions japplet

我有一个Java小程序,需要使用一些文件夹来读取某些文件等。这些文件夹位于正确的位置,与index.html(运行applet)位于同一目录中。这是JApplet吐出的错误:

java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(DeployAWTUtil.java:116)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Plugin2Manager.java:3541)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3072)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
    at java.lang.Thread.run(Thread.java:680)
Caused by: java.security.AccessControlException: access denied (java.io.FilePermission ./results read)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
    at java.security.AccessController.checkPermission(AccessController.java:546)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
    at java.io.File.list(File.java:971)
    at java.io.File.listFiles(File.java:1129)
    at WhoHasMore.JShellFrame.getDefaultResultsFileName(JShellFrame.java:453)
    at WhoHasMore.JShellFrame.<init>(JShellFrame.java:28)
    at WhoHasMore.<init>(WhoHasMore.java:23)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at sun.plugin2.applet.Plugin2Manager$13.run(Plugin2Manager.java:3060)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:199)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:677)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:647)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception: java.lang.reflect.InvocationTargetException

如您所见,错误似乎来自

Caused by: java.security.AccessControlException: access denied (java.io.FilePermission ./results read)

为什么访问被拒绝从名为“results”的文件夹中读取?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您尝试使用的方法(java.io.File.listFiles)无法通过HTTP运行。您需要选择另一种列出文件的方式,例如将文件列表放在已知URL的文本文件中并下载,或者将其作为applet jar文件的一部分。

答案 1 :(得分:0)

  

为什么访问被拒绝从名为“results”的文件夹中读取?

Java沙箱阻止applet访问用户计算机上的文件系统。

  

我该如何解决这个问题?

如果您希望applet能够执行此类操作,则必须:

  • 创建已签名的JAR,
  • 让用户接受证书/您的JAR文件作为受信任的应用程序。