Java to javascript converter

Much of today's application development is happening in the browser and it's a big advantage to use the same language for client/server applications. To address this need, StrataCode offers a robust conversion of Java to Javascript, generating readable, debuggable javascript that is close to what a Java programmer would have written anyway (i.e. using the same names in the same order, indented properly, etc).

Since layers can run on the client, server or both, StrataCode supports isomorphic Java. Web page requests generate HTML on the server so the browser draws it as quickly as possible. The page then begins loading the javascript to perform any dynamic HTML updates. You write the code once, StrataCode generates the client and server versions.

StrataCode provides schtml and scss formats which extend html and css to allow dynamic updates. Some pages are fully reactive - i.e. generated entirely from data binding, and others generate HTML from a mix of data binding and code. If necessary, you can mix Java and HTML using the full power of Java. You have inheritance, layers, data binding, tag macros, and java when you need it.

The schtml language supports synchronization which allows a template, or a tag object component of a template to run only on the server, only on the client, or on both client and server. When the code for a tag object is running on the client only, there's no code generated on the server at all - a pure javascript app is generated. When the code for a tag object is generated on the server only, it supports the same incremental updates and interactivity as the client-only version. The only difference is that the code to perform the updates is running on the server. Event generating tags on the client such as an input tag's value or a select tag's selectedIndex are sent over to the server as they change. Data binding events fire there to cause the changed parts of the template to be re-rendered and synchronized back to the client on the next sync, where those changes are applied.

When your entire web page is on the server, the system avoids downloading the Java utilities so the download size is small (~14k) but offers most of the same features using a JS native implementation of sync.

Read more in the documentation.

Here's an example. This java implementation of selection sort:

file: example/simple/SortTest.scj
public class SortTest {
   public static void doSort(int[] arr) {
      for (int i = 0; i < arr.length - 1; i++) {
         int index = i;
         for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[index])
               index = j;
         }
         int toMove = arr[index]; 
         arr[index] = arr[i];
         arr[i] = toMove;
       }
   }
     
   @sc.obj.MainSettings
   public static void main(String args[]) {
       int[] arr = {5, 8, 0, 13, 21, 1, 2, 34, 3, 1}; 
       doSort(arr);

       for (int i : arr) {
           System.out.print(i + ", ");
       }
       System.out.println();
   }
}
is converted to this javascript:
file: example/simple/js/types/example_simple_SortTest.js
// Generated JS from Java: example.simple.SortTest -----
function example_simple_SortTest() {   jv_Object.call(this);

}

var example_simple_SortTest_c = sc_newClass("example.simple.SortTest", example_simple_SortTest, jv_Object, null);

example_simple_SortTest_c.doSort = function (arr)  {
   for (var i = 0; i < arr.length - 1; i++) {
      var index = i;
      for (var j = i + 1; j < arr.length; j++) {
         if (arr[j] < arr[index])
            index = j;
      }
      var toMove = arr[index];
      arr[index] = arr[i];
      arr[i] = toMove;
   }
};
example_simple_SortTest_c.main = function (args)  {
   var arr =[5, 8, 0, 13, 21, 1, 2, 34, 3, 1];
   example_simple_SortTest_c.doSort(arr);
   {
      var _lv = arr;
      for (var _i = 0; _i < _lv.length; _i++) {
         var i = arr[_i];
         jv_System_c.out.print(i + ", ");
      }
   }
   jv_System_c.out.println();
};

Status

The Java to Javascript converter is a healthy beta. Because most Java code depends on runtime libraries that may not be available, it's not a seamless way to run any Java code in the browser. We've tested it on over 100K lines of Java without workarounds, adding any needed classes from the JDK as required. Currently it uses the Apache java utilities which support only Java 1.6 features. Java 1.8 Lambdas have been implemented in the code-generation, but not tested due to the missing libraries.

The converter uses a relatively high level of emulation of native Java classes to keep download size manageable. Right now, the code is still readable and no optimizations to detect and remove unused code have been applied so they can even be a lot better.

Currently, your entire JS app is downloaded after the page is rendered but the layered organization would be a perfect way to separate subsets of layers to be progressively loaded. Just mark layers in your stack as 'download layers' and the rest could be automated. For large client applications, you'll be able to start interacting with the app with the base layers, while successive layers are downloaded. The static type system would help manage the dependencies to be sure each individual stack defined a valid application and framework hooks installed to help provide a smooth transition from one to the next.