Skip to content

Running Java methods from JavaScript

John Vilk edited this page Nov 30, 2015 · 5 revisions

Running Java-methods from JavaScript

If you want to use DoppioJVM to run Java methods or using Java classes, you are on the right page. Here you can find out step-by-step guide how to build DoppioJVM and how to use this build for running Java from JavaScript in browser. Also there are notices which extend Doppio Developer Guide.

Clone and compile DoppioJVM

At first you have to build DoppioJVM. Just clone it from the GitHub repository and create a release build of it. You can find out how to build Doppio on the Doppio's README.

Be sure that you are using NodeJS v0.12 or higher.

Now you have a built copy of DoppioJVM in the build/release folder. There is vendor subfolder that contains additional required libraries and files.

Configure http-server

You can use any HTTP-server to run DOppioJVM, but for this guide the simplest server to use is the NodeJS http-server.

You can install http-server using the command npm install -g http-server.

Create and copy files for DoppioJVM example

Just create a folder where you will put all files for this example. Let's call it doppio-example.

Copy doppio/build/release to doppio-example. Rename release to doppio, which makes the example easier to follow.

Then, install BrowserFS by running the following command inside doppio-example:

npm install browserfs

Create a file doppio-example\index.html, which will contain all frontend configuration. Put the following code inside of it:

<html>
<head>
  <script type="text/javascript" src="node_modules/browserfs/dist/browserfs.min.js"></script>
  <script type="text/javascript" src="doppio/doppio.js"></script>
  <script type="text/javascript">
    // create a virtual file system for frontend JS
    var mfs = new BrowserFS.FileSystem.MountableFileSystem();
    var fs = BrowserFS.BFSRequire('fs');
    BrowserFS.initialize(mfs);

    // store files of folder /tmp in memory
    mfs.mount('/tmp', new BrowserFS.FileSystem.InMemory());
    // store files of folder /home in LocalStorage
    mfs.mount('/home', new BrowserFS.FileSystem.LocalStorage());
    // retrieve files of folder /sys via XmlHttpRequest according records of doppio/listings.json
    // all this file-paths will be prefixed with 'doppio/'
    mfs.mount('/sys', new BrowserFS.FileSystem.XmlHttpRequest('listings.json', 'doppio/'));

    // initialise JVM with next settings
    new doppio.JVM({
      bootstrapClasspath: ['/sys/vendor/java_home/classes'],
      classpath: ['/sys'],
      javaHomePath: '/sys/vendor/java_home',
      extractionPath: '/tmp',
      nativeClasspath: ['/sys/natives'],
      assertionsEnabled: false
    }, function(err, jvmObject) {
      // here you can use jvmObject as Java Virtual Machine
    });      
  </script>
</head>
<body></body>
</html>

When you have completed the above, you can try it by running the following commands:

cd <PATH_TO_DOPPIO_EXAMPLE>
http-server

After this open your browser and go to the page http://localhost:8080. If there are no errors in the developer console of your browser so you've done everything correctly.

Create Java class, compile it and make accessible to DoppioJVM

Now we will write a simple Java class. Let's name it SimpleExample.java. This class contains only one static function that returns a String value:

public class SimpleExample {
  public static String hello() {
    return "Hello from Java.";
  }
}

Compile it using javac. And after this you will have a file SimpleExample.class. Put this file into the doppio-example\doppio folder.

Next, we need to make this file accessible from the virtual /sys folder. So we have to change our listings.json file to make it accessible to BrowserFS. Just add "SimpleExample.class":null to listings.json. And the beginning of this file will look like:

{"SimpleExample.class":null, "browser":{"fs.js":null,"path.js":null,"require_config.js"...

Now DoppioJVM can download and use this file as a Java class.

Run Java method and use returned value

We have to use jvmObject to initialise the class SimpleExample and run its static method. Just insert this code to callback-function of JVM initialisation:

// look for class and initialise this
jvmObject.getSystemClassLoader().initializeClass(jvmObject.firstThread, 'LSimpleExample;', function(cls) {
  // look for method and run this
  var m = cls.methodLookup(jvmObject.firstThread, 'hello()Ljava/lang/String;');
  jvmObject.firstThread.runMethod(m, [], function (e, rv) {
    // read rv (returned value) and compile it to regular JavaScript String
    // toString() will do this easily
    var message = rv.toString();
    alert(message);
  });
});

Now when you open http://localhost:8080 browser alerts the message "Hello from Java.".