Running Java in JavaScript

Welcome Waxy.org and Slashdot readers. I blog about JavaScript, like it’s my job, feel free to subscribe for a ton more posts like this.

Related Posts:

When I was in Tokyo this past fall speaking at the Shibuya.JS user group I had the opportunity to see a number of interesting JavaScript projects that have yet to make it outside of Japan.

One project, in particular, really caught my eye. It’s called Orto [PDF, Japanese] and is an implementation of the Java Virtual Machine (JVM) in JavaScript. This means that you can take an existing Java application, compile it to bytecode, run it through Orto (which produces the JavaScript, and embed it in a web page. While it doesn’t provide the full capabilities of most Java code it does provide enough to make for some interesting demos.

The one demo that was presented was that of a real-time, interactive, Tetris game:

If you take a peak at the generated source code you can see some interesting tidbits (obviously all of the code is meant to be ‘machine readable’ – being just converted bytecode):

"java/lang/Thread 1316742099":function(){var orto333=orto245[0];
var orto336=orto350(orto333);
if(orto336.orto340!=orto310){orto223("java/lang/IllegalThreadStateException",null);
return ;
}
case 117:orto246[orto247-2]={high:(~orto246[orto247-2].high)
  &0xffffffff,low:(~orto246[orto247-2].low+1)&0xffffffff};
if(orto246[orto247-2].low==0){orto246[orto247-2].high++;
orto246[orto247-2].high&=0xffffffff;
orto246[orto247-2].low=0;
}break;
case "CHECKBOX":orto171=orto188["orto/ui/CheckBox"];
break;
case "IMAGE":orto171=orto188["orto/ui/ImageButton"];
break;
case "RADIO":orto171=orto188["orto/ui/RadioButton"];
break;

Unfortunately, the information on the subject is a little scarce (considering that both orto.accelart.jp and orto.jp seem to be missing any relevant information). There are a couple of things that are apparent, however:

  • The result is able to handle threaded application code (translating the threads into a series of yields with setTimeout (mentioned in the presentation and demonstrated in the Tetris example).
  • The application can use regular Java conventions for designing and constructing the UI (as shown here, as well). User Interface components are translated to, similar, HTML ones. It’s not apparent to what extent functionality is implemented, but it is to a certain degree.
  • Keyboard interactions are able to be handled and translated to normal Java callbacks.

I was especially pleased with the resulting performance of the demo application. I wasn’t expecting a game (translated multiple layers deep) to still be as playable as it is. Obviously the culmination of JavaScript optimization and increases in browser performance have served these types of endeavors well.

Posted: April 28th, 2008


Subscribe for email updates

25 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

John Resig Twitter Updates

@jeresig / Mastodon

Infrequent, short, updates and links.