1

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

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Murrah
  • 1,508
  • 1
  • 13
  • 26
  • I finally got this working. See [http://stackoverflow.com/questions/16313702/javascript-to-java-applet-working][1] for a working example. [1]: http://stackoverflow.com/questions/16313702/javascript-to-java-applet-working – Murrah May 01 '13 at 06:30

2 Answers2

1

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

Launches the associated application to open the file.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

Ok, @Andrew. Some progress, thank you!

I set the default program for *.js files to Eclipse and if I double click a file it opens in Eclipse. All good.

I then had success running the following using RunAs Java Application - the test file opened in Eclipse. Getting closer!

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

    }}

However, when I added the following method and ran it via the DeployJava.js (as per my original post above), I get the following output returned with the error appearing whether or not the jar is self signed.

Started: , Desktop is supported , Error: java.security.AccessControlException: access denied ("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;
}

So, what do I need to add to get around the apparent security issue? I have read the docs and the tutorials and I am going around in circles! There seems to be a lot of conflicting advice. :-(

Thanks again, Murray

Murrah
  • 1,508
  • 1
  • 13
  • 26
  • *"Ok, @Andrew. Some progress, thank you!"* If this is an answer you should accept it. If it is not, you should edit it into the question (delete the 'not an answer') and notify me in a comment. – Andrew Thompson Apr 29 '13 at 03:26
  • Thanks Andrew. I was indeed going to do that and couldnt see how to add the new code without making the post completely confusing since I changed my approach based upon your helpful advice. Sorry about that. And... as you can see in my post it is not an answer yet since I still cant get it to work with DeployJava.js as per the original post. Any further help you could give me would be very welcome. Thanks. – Murrah Apr 29 '13 at 07:30
  • AND..... I finally got this working: See [link](http://stackoverflow.com/questions/16313702/javascript-to-java-applet-working) – Murrah May 01 '13 at 06:27