Search code examples
javajavascriptappletruntime.execdeployjava

JavaScript to Java Applet using DeployJava.js to run commandline


I am pretty new to Java. I want to create a Java Applet that will allow my JavaScript to pass a commandline to the 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. 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.

I am using Win7x64, jre 1.7

So, to get Eclipse to open the file from the commandline the command is:

D:\Eclipse\eclipse.exe --launcher.openFile C:\mytestfile.js

This works.

I have written the Applet, self signed it and tested the say() method using the code shown below. That works. However when I run the executecmd() method, I don't get any output. If I comment out the whole try/catch block so that I am simply returning the cmd string passed in, the method works. Therefore, I suspect that I have the try catch incorrectly setup and since my Java skills and knowledge of the exceptions are primitive I am lost.

Can anyone help me please? At least to get some output returned, if not how to actually run the command line passed in?

And, I am passing the whole command line because when I have this working I would like to share it (since the Ext introspector is really useful). Other developers will be using different editors so this way they can use it by passing their specific commandline.

Thanks! Murray

My HTML test page:

<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>

My class:

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

}

Solution

  • Set Eclipse as the default consumer for .java files and use Desktop.open(File) which..

    Launches the associated application to open the file.