【问题标题】:JavaScript to Java Applet using DeployJava.js to run commandlineJavaScript to Java Applet 使用 Deploy Java.js 运行命令行
【发布时间】:2013-04-24 03:26:10
【问题描述】:

我对 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 内省器非常有用)。其他开发人员将使用不同的编辑器,因此他们可以通过传递特定的命令行来使用它。

谢谢! 穆雷

我的 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;
    }

}

【问题讨论】:

标签: java javascript applet runtime.exec deployjava


【解决方案1】:

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

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

【讨论】:

  • 谢谢。那是一种不同的方法。我去看看。
【解决方案2】:

好的,@安德鲁。有进步了,谢谢!

我将 *.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 是否是自签名。

已启动:,支持桌面,错误: 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;
}

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

再次感谢, 穆雷

【讨论】:

  • “好的,@Andrew。有些进展,谢谢!” 如果这是一个答案,你应该接受它。如果不是,您应该将其编辑到问题中(删除“不是答案”)并在评论中通知我。
  • 谢谢安德鲁。我确实打算这样做,并且看不到如何添加新代码而不会使帖子完全混乱,因为我根据您的有用建议改变了我的方法。对于那个很抱歉。而且......正如您在我的帖子中看到的那样,这还不是一个答案,因为我仍然无法按照原始帖子使用 DeployJava.js。您可以给我的任何进一步帮助将非常受欢迎。谢谢。
  • 我认为你应该accept my answer 并在AccessControlException 上开始一个新问题。不要忘记这些 Q&A 应该对以后搜索的人有所帮助,因此将它们全部混合成“如何修复我的小程序?”没有任何好处。不过,在你问这个问题之前,请在[applet] [javascript] AccessControlException 上搜索 SO - 答案(可能是我)应该在前几个点击中。
  • AND.....我终于得到了这个工作:见link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-15
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
相关资源
最近更新 更多