简体   繁体   English

JavaScript到Java小程序-工作

[英]JavaScript to Java applet - working

This is a continuation of my original post here: javascript-to-java-applet-using-deployjava-js-to-run-commandline 这是我原来的帖子的继续: javascript-to-java-applet-using-deployjava-js-to-run-commandline

I am pretty new to Java. 我对Java很陌生。 I want to create a Java Applet that will allow my JavaScript to pass a commandline to the Java Applet. 我想创建一个Java Applet,它将允许我的JavaScript将命令行传递给Java Applet。 This will only ever be run on my development machine - no need to remind me what a security issue that is. 这只会在我的开发计算机上运行-无需提醒我什么是安全问题。 The use-case is that I have an introspector for my ExtJS app that allows me to display the classes. 用例是我的ExtJS应用程序有一个内省器,可以显示类。 I want to be able to click a class, pass the relevant pathname to the Applet and have that file open in Eclipse for editing. 我希望能够单击一个类,将相关的路径名传递给Applet,并在Eclipse中打开该文件进行编辑。

After many failed tests this is what I found works. 经过多次失败的测试后,我发现这是可行的。 Thanks to Andrew Thompson and others mentioned below. 感谢Andrew Thompson和下面提到的其他人。

It seems there are two paths and I have managed to get them both working. 看来有两条路径,而且我设法使它们都起作用。 I include them both here. 我将它们都包括在这里。 Path 1 is to execute a program with parameters (eg D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js ) and Path 2 is to set Win7 to open Eclipse when a *.js file is opened (ie associate *.js with Eclipse). 路径1用于执行带有参数的程序(例如D:/Eclipse/eclipse.exe --launcher.openFile C:/sites/test.js ),路径2用于将Win7设置为在* .js文件被打开时打开Eclipse。打开(即,将* .js与Eclipse关联)。

SECURITY DISCLAIMER: Remember that Path 1 is completely insecure on a public server - it would be relatively easy to pass a format or delete command or any other mischief via the JavaScript!! 安全免责声明:请记住,路径1在公共服务器上是完全不安全的-通过JavaScript传递格式或删除命令或其他任何恶作剧相对容易!

For Java newbies like me I will be as explicit as I can about the steps. 对于像我这样的Java新手,我将尽可能明确地介绍这些步骤。

Class for executing programs. 用于执行程序的类。 Thank you to: https://stackoverflow.com/users/80389/corgrath 感谢您: 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();
        }

    }

}

Class for running programs associated with files: 用于运行与文件关联的程序的类:

    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();
        }

    }

}

Using Eclipse, I exported those two classes to one jar file called runcombo.jar located in the same folder as the following HTML file. 使用Eclipse,我将这两个类导出到一个名为runco​​mbo.jar的jar文件中,该文件与以下HTML文件位于同一文件夹中。 I then self signed the jar which is necessary for the security issues also. 然后,我对罐子进行了自我签名,这对于安全性问题也是必需的。 I found this tutorial helpful with that process. 我发现本教程对该过程有所帮助。 http://www.jade-cheng.com/uh/ta/signed-applet-tutorial/ . http://www.jade-cheng.com/uh/ta/signed-applet-tutorial/

The HTML and JavaScript test page: 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>

Note that the applet is added to the DOM when the JS functions are called. 请注意,当调用JS函数时,小程序将添加到DOM中。 This is part of working with the Java security and avoids the aforementioned security issues that stopped the applet from running. 这是使用Java安全性的一部分,避免了前面提到的导致applet无法运行的安全性问题。 Note also that there are two JS function calls to match the different classes. 还要注意,有两个JS函数调用来匹配不同的类。

Thanks again to everyone who helped my get this working. 再次感谢所有帮助我完成这项工作的人。 Now I can get back to the original purpose - to finish my Ext Introspector! 现在,我可以回到原来的目的-完成我的Ext Introspector!

Murray 默里

you do not need to make a new instance of the applet every time, using the mayscript tag for applet you can tell the browser to allow java script to applet communication. 您不需要每次都为applet创建新实例,使用mayscript标签作为applet可以告诉浏览器允许Java脚本与applet通信。

Also you should clean up the init method 另外你应该清理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 and http://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/java_js.html The old netscape classes are part of every JVM (netscape.javascript.) 参考http://help.dottoro.com/lhbkaqko.phphttp://docs.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/java_js.html旧的netscape类是每个类的一部分JVM(netscape.javascript。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM