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 sccss 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. You can also enable synchronization and so run the same code in client-only, server-only, and client-server versions via different build configurations. Read more in the documentation.

It's also possible to use the Java to JS converter by itself.

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 = sc_initArray(Number_c, 1, [ 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-gen by converting to inner classes, but that conversion has not been tested due to the missing libraries.

The converter uses a relatively high level of emulation of native Java classes to keep download size manageable. It's easy to replace any class with a native version using an annotation. Right now, the code is still readable and no optimizations to remove unused code so lots of future optimization potential even though performance is not bad.

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.