1

我对Java很陌生。我想创建一个 Java Applet,它允许我的 JavaScript 将命令行传递给 Java Applet。这只会在我的开发机器上运行 - 无需提醒我这是一个安全问题。用例是我的 ExtJS 应用程序有一个内省器,它允许我显示类。我希望能够单击一个类,将相关路径名传递给 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;
    }

}
4

2 回答 2

1

将 Eclipse 设置为.java文件的默认使用者并使用Desktop.open(File)which..

启动关联的应用程序以打开文件。

于 2013-04-24T03:29:34.623 回答
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 是 self signed

开始:,支持桌面,错误: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;
}

那么,我需要添加什么来解决明显的安全问题?我已经阅读了文档和教程,并且正在转圈!似乎有很多相互矛盾的建议。:-(

再次感谢,默里

于 2013-04-25T10:40:43.390 回答