首先,我知道我的问题与此处相同。但是这个问题对我没有帮助。
我有自签名小程序。
jarsigner -verify sJSApplet.jar
jar verified.
Warning:
This jar contains entries whose signer certificate will expire within six months.
Applet 的目的是从 LAN 机器上打开 MS Word 文档。到目前为止,我已经尝试使用Desktop.open()
and打开Runtime.exec()
。AccessController.doPrivileged
有无。我总是得到java.security.AccessControlException: access denied
。
我别无选择。我还能做什么?
我不能使用 java.policy 文件。
HTML
<html>
<head>
<script>
function openFile( command ) {
var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='com.avacoda.swing.JSApplet'/><param name='archive' value='sJSApplet.jar' /><param name='mayscript' value='true'/><param name='filePath' value='C:\\note.txt'/>Applet failed to run. No Java plug-in was found.</object>";
var body = document.getElementsByTagName("body")[0];
var div = document.createElement("div");
div.innerHTML = applet;
body.appendChild(div);
}
</script>
</head>
<body>
<a href="#" onclick="openFile('C:/note.txt');">Open file</a>
</body>
</html>
Java代码:
public class WordApplet extends JApplet {
@Override
public void init() {
openFile(getParameter("filePath"));
};
public void openFile(final String path) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Runtime.getRuntime().exec("winword " + path);
//Desktop.getDesktop().open(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
});
}
}
完整的堆栈跟踪
java.security.AccessControlException: access denied (java.io.FilePermission <<ALL FILES>> execute)
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.checkExec(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at com.test.applet.JSApplet$1.run(JSApplet.java:34)
at java.security.AccessController.doPrivileged(Native Method)
at com.avacoda.swing.JSApplet.openFile(JSApplet.java:29)
at com.avacoda.swing.JSApplet.init(JSApplet.java:25)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
basic: Applet initialized