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. StrataCode generates readable, debuggable javascript that is close to what a Java programmer would have written anyway (i.e. using the same names, the same order in the file, formatted, 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 version of the code 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.

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 is dependent 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 stoped evolving at Java 1.6. Java 1.8 Lambdas have been implemented in the code-generation, but not really tested due to the missing libraries.

No optimization has been done yet to remove unused code or optimize download size but those are planned. There's a flexible library system and the class emulation is higher than other Java to JS implementations so footprint is reduced that way.

Currently, your entire JS app is downloaded after the page is rendered but we plan to allow separate subsets of layers to be progressively loaded. For large client applications, you'll be able to start interacting with the app with the base layers, while successive layers are downloaded.