我对Java很新。我想创建一个Java Applet,它允许我的JavaScript将命令行传递给Java Applet。这只会在我的开发机器上运行 - 无需提醒我安全问题是什么。用例是我的ExtJS应用程序有一个introspector,允许我显示类。我希望能够单击一个类,将相关的路径名传递给Applet,并在Eclipse中打开该文件进行编辑。
我正在使用Win7x64,jre 1.7
因此,要让Eclipse从命令行打开文件,命令为:
D:\Eclipse\eclipse.exe --launcher.openFile C:\mytestfile.js
这很有效。
我编写了Applet,自签名并使用下面显示的代码测试了say()方法。这样可行。但是,当我运行executecmd()方法时,我没有得到任何输出。如果我注释掉整个try / catch块,以便我只返回传入的cmd字符串,则该方法有效。因此,我怀疑我的try catch设置错误,因为我的Java技能和异常知识是原始的,我迷失了。
有人能帮帮我吗?至少要返回一些输出,如果不是如何实际运行传入的命令行?
并且,我正在传递整个命令行,因为当我有这个工作时,我想分享它(因为Ext introspector非常有用)。其他开发人员将使用不同的编辑器,因此他们可以通过传递他们的特定命令行来使用它。
谢谢! 默里
我的HTML测试页:
<html>
<head>
<meta charset="UTF-8">
<title>Test Run</title>
<script src="http://www.java.com/js/deployJava.js"></script>
<script>
var attributes = { id:'testapp', code:'systemcmd.Runcmd', archive:'runcmd.jar', width:300, height:50} ;
var parameters = {} ;
deployJava.runApplet(attributes, parameters, '1.6');
</script>
</head>
<body>
<p>Hello</p>
<script type="text/javascript">
//alert(testapp.say("Hello test")); // This works
var command = "D:\Eclipse\eclipse.exe --launcher.openFile C:\mytestfile.js";
alert(testapp.executecmd(command)); // Nothing returned at all.
</script>
</body>
</html>
我的课程:
package systemcmd;
import java.applet.Applet;
import java.io.IOException;
import java.security.AccessController;
//import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
public class Runcmd extends Applet {
private static final long serialVersionUID = -4370650602318597069L;
/**
* @param args
*/
public static void main(String[] args) {
}
public String say(String arg)
{
String msg[] = {null};
msg[0] = "In Say. You said: " + arg;
String output ="";
for(String str: msg)
output=output+str;
return output;
}
public String executecmd(final String cmd) throws IOException
{
final String msg[] = {null};
String output ="";
msg[0] = "In executecmd, cmd="+cmd;
try {
try {
AccessController.doPrivileged(
new PrivilegedExceptionAction() {
public Object run() throws IOException { //RuntimeException,
msg[1] = " Pre exec()";
Runtime.getRuntime().exec(cmd);
msg[2] = " Post exec()";
return null;
}
}
);
} catch (PrivilegedActionException e) {
msg[3] = " Caught PrivilegedActionException:"+ e.toString();
throw (IOException) e.getException();
}
}
catch (Exception e) {
msg[4] = " Command:" + cmd + ". Exception:" + e.toString();
}
msg[5] = " End of executecmd.";
for(String str: msg)
output=output+str;
return output;
}
}
答案 0 :(得分:1)
将Eclipse设置为.java
文件的默认使用者,并使用Desktop.open(File)
...
启动关联的应用程序以打开该文件。
答案 1 :(得分:0)
好的,@安德鲁。一些进步,谢谢!
我将* .js文件的默认程序设置为Eclipse,如果我双击它在Eclipse中打开的文件。一切都好。
然后,我成功使用RunAs Java Application运行以下内容 - 在Eclipse中打开的测试文件。越来越近了!
public class Runcmd extends Applet {
File file;
private static Desktop desktop;
private static final long serialVersionUID = -4370650602318597069L;
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
System.out.println("hello");
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
File file = new File("C:\\sites\\test.js");
// This works if I execute it from the Eclipse RunsAs Java Application.
// ie the file is opened in Eclipse for editing.
// And, if I specify a non-existant file, it correctly throws and prints the error
try {
desktop.open(file);
} catch (Exception ioe) {
ioe.printStackTrace();
System.out.println("Error: " + ioe.toString());
}
}}
但是,当我添加以下方法并通过DeployJava.js运行它时(根据我上面的原始帖子),我得到以下输出,并显示出错是否jar是自签名的< / em>的
已启动:,支持Desktop,错误: java.security.AccessControlException:访问被拒绝 (“java.awt.AWTPermission”“showWindowWithoutWarningBanner”)
public static String openfile(String arg) {
String output = "Started: ";
File file = new File("C:\\sites\\test.js");
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
output = output + ", Desktop is supported ";
}
try {
desktop.open(file);
} catch (Exception ioe) {
output = output + ", Error: " + ioe.toString();
}
return output + arg;
}
那么,我需要添加什么才能解决明显的安全问题?我已经阅读了文档和教程,我正在圈子里去看看!似乎有很多相互矛盾的建议。 : - (
再次感谢, 默里