kritya
kritya

Reputation: 3362

How to execute Java file from JavaScript on client's side

I have a code/class/script in JAVA that I want to be executed when someone clicks on a button/anything that I will handle. What should be the code in JavaScript to launch that class/code/script/compiled program installed on the client's system?

Upvotes: 1

Views: 10494

Answers (2)

Jeff Huff
Jeff Huff

Reputation: 46

It depends on what you are trying to do with the program on the client-side. If you want to call other programs on the client's machine, you will need a few things.

  1. A code-signing certificate from an SSL vendor.
  2. You need to create an class that extends Applet.
  3. For just about anything you want to do on that client machine you need a class that implements java.security.PrivilegedAction.
  4. You should really use the objet and embed tags instead of applet now.
  5. Add the attributes to both the object and embed tags that allow scripting from Javascript.

Java code

public class MyApplet extends Applet
{
   public Object myScriptAction(String arg1, String arg2)
   {
       PrivilegedAction action = new MyAction();
       action.setXXXX();
       Object o = AccessController.doPrivileged(action);
       return o;
   }

   private class MyAction implements PrivilegedAction
   {
       public MyAction() {}
       ...
       public Object run()
       {
           // Do something here, Runtime.exec() or something else
           Runtime.getRuntime().exec(cmdarray, envarray, workingDir);
           return myobj;
       }
   }
}

HTML code with tags removed

    object name="myapplet" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="1" height="1"
    param name="name" value="myapplet"
    param name="code" value="com.example.MyApplet"
    param name="codebase" value="<c:out value="/path/to/applet/"
    param name="archive" value="myapplet.jar"
    param name="scriptable" value="true"
    param name="mayscript" value="true"
    comment
        embed
            type="application/x-java-applet;version=1.5"
            code="com.example.MyApplet"
            java_codebase="/path/to/applet/"
            archive="myapplet.jar"
            name="myapplet"
            width="1"
            height="1"
            mayscript="true"
            scriptable="false"
            pluginspage="http://java.sun.com/products/plugin/index.html#download"
            noembed
                    Java not supported.
            noembed
        embed
     comment
object

The key is the mayscript and scriptable attributes that allow Javascript to call an applet.

You then use jarsigner to sign the myapplet.jar with the code-signing cert you get.

See this guide from Sun on mixed-browser Java plugin support.

http://java.sun.com/j2se/1.5.0/docs/guide/plugin/developer_guide/using_tags.html#mixed

Upvotes: 2

AlexR
AlexR

Reputation: 115328

I think that you have to deploy applet on your web page and then be able to call its method from javascript.

So, first you need appelet, i.e. class that extends Applet. If you already have one, you are done. Otherwise you have to create one and expose API that you already have. Next you should deploy the applet on web page. Use either tag <applet> or <object> to do this. It is simple and you can find a lot of examples in internet.

Now you have to be able to call this applet from javascript. It is simple too. if for example you applet's name (I mean value of html attribute name) is myapp and the applet has method foo() you can invoke it from javascript like this: myapp.foo()

Here is an example:

<applet name="myapp" archive="myjar.jar" code="com.company.MyApplet"/>
<script>
    var result = myapp.foo();
</script>

Upvotes: 2

Related Questions