我創建簡單的Java類,測試文件的小程序寫道:
更新出現麻煩從Java小程序內以書面文件
public class localfile extends Applet{
public localfile(){
try {
File f = new File("testfile.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(f,true));
out.write("test");
out.close();
}
catch(Exception x)
System.err.println(x.toString());
}
}
我已經創建並簽名的jar:
jar cvf localfile.jar localfile.java
jarsigner localfile.jar yourkey
html的樣子: <applet code="localfile.class" archive="localfile.jar", width=300, height=600
>
錯誤我得到我每次運行此applet時間:
java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.check(CPCallbackHandler.java:308)
at com.sun.deploy.security.CPCallbackHandler$ParentCallback.access$1400(CPCallbackHandler.java:121)
at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(CPCallbackHandler.java:473)
at sun.plugin2.applet.Plugin2ClassLoader.checkResource(Plugin2ClassLoader.java:701)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:206)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:520)
at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:2940)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1444)
at java.lang.Thread.run(Thread.java:619)
Exception: java.lang.SecurityException: trusted loader attempted to load sandboxed resource from file:/home/w/test/
什麼奇怪的是:我已經建立了類似的小程序來讀取文件,它工作正常。
有什麼想法?
我同時運行瀏覽器和applet瀏覽器這個小程序。奇怪的applet不適用於applet查看器並拋出異常,但是在瀏覽器中它很好。
java.security.AccessControlException: access denied (java.util.PropertyPermission java.security.policy write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.System.setProperty(System.java:725)
at localfile.<init>(localfile.java:15)
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.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:619)
所以,除了這種奇怪的行爲,我認爲我的問題解決了。謝謝大家:)
不幸的是,Java WebStart不是一個選項:/添加一些導入applet編譯後,但出現新的錯誤(訪問被拒絕),我正在調查。 – wlk 2010-06-21 21:17:33
我有同樣的問題,但與JNLP /「WebStart」,所以這可能不是一個好的解決方案。 – 2013-07-03 18:08:01