JavaScript到Java小程序-工作

JavaScript to Java applet - working

本文关键字:工作 程序 Java JavaScript      更新时间:2023-09-26

这是我在这里的原始帖子的延续:javascript-to-java-applet-using-deployjava-js-to-run-commandline

我对Java很陌生。我想创建一个Java Applet,允许我的JavaScript将命令行传递给Java Applet。这只会在我的开发机器上运行——不需要提醒我这是什么安全问题。用例是,我有一个内省器为我的ExtJS应用程序,允许我显示类。我希望能够单击一个类,将相关的路径名传递给Applet,并在Eclipse中打开该文件进行编辑。

经过多次失败的测试后,我发现这是有效的。感谢Andrew Thompson和下面提到的其他人。

似乎有两条路,我已经设法让他们都工作。我把它们都包括在这里。路径1是执行带参数的程序(如D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js),路径2是设置Win7在打开*.js文件时打开Eclipse(即将*.js与Eclipse关联)。

安全声明:请记住,路径1在公共服务器上是完全不安全的-通过JavaScript传递格式或删除命令或任何其他恶作剧相对容易!!

对于像我这样的Java新手,我会尽可能明确地说明步骤。

用于执行程序的类。谢谢:https://stackoverflow.com/users/80389/corgrath
    package systemcmd;

import java.applet.Applet;
import java.io.*;
public class Runcmd extends Applet {
    private static final long serialVersionUID = 1L;
    public void init() {
        // It seems that even though this command is not executed when the
        // applet is run via JS we still need to refer to the exec command here,
        // I presume so it is linked? If I comment these out altogether, the
        // applet doesnt work. Either of the following will suffice.
        // FYI. Just betraying my Java newbie status! :-)
        //exec("notepad c:/sites/test.txt");
        exec("calc");
    }
        // From https://stackoverflow.com/questions/1068271/signed-applet-gives-accesscontrolexception-access-denied-when-calling-from-jav
        // Thank you !!!
    public void exec(String command) {
        try {
            // launch EXE and grab stdin/stdout and stderr
            Process process = Runtime.getRuntime().exec(getParameter("command"));
            //      OutputStream stdin = process.getOutputStream();
            InputStream stderr = process.getErrorStream();
            InputStream stdout = process.getInputStream();
            // clean up if any output in stdout
            String line = "";
            BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(stdout));
            while ((line = brCleanUp.readLine()) != null) {
                //System.out.println ("[Stdout] " + line);
            }
            brCleanUp.close();
            // clean up if any output in stderr
            brCleanUp = new BufferedReader(new InputStreamReader(stderr));
            while ((line = brCleanUp.readLine()) != null) {
                //System.out.println ("[Stderr] " + line);
            }
            brCleanUp.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

用于运行与文件相关的程序的类:

    package systemcmd;

import java.applet.Applet;
import java.awt.Desktop;
import java.io.*;
public class Launchapp extends Applet {
    private static final long serialVersionUID = 1L;
    public void init() {
        // It seems that even though this command is not executed when the
        // applet is run via JS we still need to refer to the exec command here,
        // I presume so it is linked? If I comment these out altogether, the
        // applet doesnt work. Either of the following will suffice.
        // FYI. Just betraying my Java newbie status! :-)
        //launch("notepad c:/sites/test.txt");
        launch("calc");
    }
        // From https://stackoverflow.com/questions/1068271/signed-applet-gives-accesscontrolexception-access-denied-when-calling-from-jav
        // Thank you !!!
    public void launch(String command) {
        try {
            Desktop.getDesktop().open(new File(getParameter("command")));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}

使用Eclipse,我将这两个类导出到一个名为runcombo.jar的jar文件中,该文件位于与以下HTML文件相同的文件夹中。然后,我对jar进行了自签名,这也是安全问题所必需的。我发现这个教程对这个过程很有帮助。http://www.jade-cheng.com/uh/ta/signed-applet-tutorial/。

HTML和JavaScript测试页:

    <html>
    <head>
<script type="text/javascript">
    function exec( command ) {                                                                                                  
            var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='systemcmd.Runcmd'/><param name='archive' value='runcombo.jar' /><param name='mayscript' value='true'/><param name='command' value='" + command + "'/>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);
    }
    function launch( command ) {                                                                                                    
            var applet = "<object type='application/x-java-applet' height='100' width='100' name='jsApplet'><param name='code' value='systemcmd.Launchapp'/><param name='archive' value='runcombo.jar' /><param name='mayscript' value='true'/><param name='command' value='" + command + "'/>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="exec('notepad c:/sites/test.txt');">Exec Notepad</a>
        <br> <a href="#" onclick="exec('calc');">Exec Calculator</a>
        <br> <a href="#" onclick="exec('D:/Eclipse/eclipse.exe  --launcher.openFile C:/sites/test.js');">Exec Eclipse with command line parms</a>
        <br> <a href="#" onclick="launch('C:/sites/test2.txt');">Launch Notepad via *.txt association</a>
        <br> <a href="#" onclick="launch('C:/sites/test2.js');">Launch Eclipse via *.js association</a>
    </body>
</html>

注意,当调用JS函数时,applet被添加到DOM中。这是使用Java安全性的一部分,避免了前面提到的阻止applet运行的安全问题。还要注意,有两个JS函数调用来匹配不同的类。

再次感谢每个人谁帮助我得到这个工作。现在我可以回到最初的目的-完成我的Ext内省器!

穆雷

您不需要每次都创建applet的新实例,使用applet的mayscript标签您可以告诉浏览器允许Java脚本与applet通信。

你还应该清理init方法

<head>
    <script type="text/javascript">
    //whatToDo can be from an input box
    //in applet check if its del or rmdir or move or delete at a minimum
        function callJavaCmd (whatToDo) {
                // call the MessageBox method of the applet
            var applet = document.getElementById ("myApplet");
            applet.command (whatToDo);
        }
    </script>
</head>
<body>
    <applet id="myApplet" code="Your.class" codebase="/base/"
            mayscript="mayscript" style="width:300px; height:50px;">
    </applet>

Ref http://help.dottoro.com/lhbkaqko.php和http://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/java_js.html旧的netscape类是每个JVM (netscape.javascript.)的一部分