JavaPoly.js

Java(script) in the Browser

Download javapoly.js View Source on JavaDeploy

Introduction

JavaPoly.js is a library that polyfills native JVM support in the browser. It allows you to import your existing Java code, and invoke the code directly from Javascript.

JavaPoly works even if the user does not have Java installed on their computer. It finally allows Java to become a first class citizen by running directly in the browser's script tags (and interacting directly with the DOM), instead of being confined to an applet sandbox.

<!-- Include the Polyfill -->
<script src="https://www.javapoly.com/javapoly.js"></script>

<!-- Write your Java code -->
<script type="text/java">
  package com.demo;
  import com.javapoly.dom.Window;

  public class Greeter
  {
    public static void sayHello(String name)
    {
      Window.alert("Hello " + name + ", from Java!");
    }
  }
</script>

<!-- Invoke your Java code from Javascript -->
<script type="text/javascript">
  com.demo.Greeter.sayHello("world");
</script>

Libraries

Most Java libraries may be included, just as any other script would normally be included.

<!-- Include the Polyfill -->
<script src="https://www.javapoly.com/javapoly.js"></script>

<!-- Include your favorite Java libraries (jar files) -->
<script type="text/java" src="http://www.yourdomain.com/jimboxutilities.jar"></script>
<script type="text/java" src="http://www.yourdomain.com/guava.jar"></script>
<script type="text/java" src="http://www.yourdomain.com/apache-commons.jar"></script>

<!-- Or, include individual .class files -->
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Foo.class"></script>
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Noise.class"></script>

<!-- Or just include the source directly -->
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Bar.java"></script>
<script type="text/java" src="http://www.yourdomain.com/com/yourpackage/Girls.java"></script>

Any imported classes/libraries are available from javascript via their fully qualified class name.*

<script type="text/javascript">
  com.yourpackage.Foo.doSomething();
  com.yourpackage.Noise.doSomethingElse();
</script>

* Using Fully Qualified Names to reference Java classes directly from javascript does have a set of limitations (especially on legacy browsers), so JavaPoly.type() may be used as an alternative syntax for fetching references to Java classes.

Asynchronous Invocations

Javascript is single-threaded, while Java applications can be multi-threaded. To prevent the browser's UI thread from blocking while javascript waits for a return value, all calls to the JVM are asynchronous. The return value of a javascript call to Java code will always be a promise.

var promise = com.mypackage.MyClass.getSomeValue();
promise.then(function(result) {
  console.log(result); // "Stuff worked!"
}, function(err) {
  console.log(err); // Error: "It broke"
});

NOTE: It is important that you ALWAYS handle the promise, especially if the Java function could throw an exception. If you blindly discard the returned promise, then your javascript code is effectively swallowing exceptions!

Try it yourself

This interactive demo allows you to write your own Java program and execute it directly in the browser. To get started, just edit the code below and then click the "Compile & Run" button.

The demo works by starting a JavaPoly virtual machine, compiling the Java source code (using a Java compiler written in Java and running in the same virtual machine), and then executing the compiled program. This is all done on the fly, in the browser.

After you're done playing with the demo above, the next step is to download the Starter Pack, which is filled with great examples and documentation to get you started. Happy coding!

Acknowledgements

This polyfill is maintained by Harshad RJ and Jim Sproch. This polyfill would not have been possible without the incredibly awesome work of John Vilk and friends who have contributed to the doppio project.