ST-JS: Strongly Typed JavaScript

Borrowing Java's syntax to write type-safe JavaScript


This tutorial explains you how to build a "Hello World" application using ST-JS. We presume you have previous knowledge of Maven, Java, JavaScript and HTML. Additionally, you'll have to deploy the generated war file to your preferred servlet engine. Even though Java is used to generate the JavaScript, for this particular example no Java code is run, as no interaction with the server is made. So you can also take the content of the war file and deploy it to an Apache server, for example, and it will work!

If you're too busy to follow the steps, you can find this tutorial also here:

So here are the steps to create a new, very simple application, with ST-JS:

  1. Create a new empty POM file for a war project
    <project xmlns=""
        <name>Hello world</name>
  2. Add the stjs.version property - easier to have the same version between the different ST-JS artifacts
  3. Point the build to the actual source folder containing the Java source files to compile to JavaScript and set the Java version to a version superior to 1.5
  4. Add the dependency to the ST-JS JavaScript (basic and DOM) bridge library
  5. Add the ST-JS plugin that will generate the JavaScript code (inside the build/plugins section)
  6. Add the index.html page (in src/main/webapp)
        <h1>ST-JS example: Hello world</h1>
        <form id="form">
            Say hello to: <input type="text" name="to"> <input type="button" name="say" value="Go">
  7. Create the Java class org/stjs/hello/ The main method will be executed after the declaration of the class.
    public class HelloWorld {
        public static void main(String [] args){
  8. Add the onload listener in the main method. The "window" object is a static member of the Global class.
    window.onload = new Callback1<DOMEvent>() {
        public void $invoke(DOMEvent ev) {
  9. Add the click listener to the "Go" button inside the onload listener
    Form form = window.document.forms.$get(0);
    Element button = form.elements.$get("say");
    final Input text = form.elements.$get("to");
    button.onclick = (ev) -> {
        alert("Hello " + text.value);
        return true;
  10. Include the generated JavaScript and the ST-JS support file in the page
        <script src="generated-js/stjs.js" type="text/javascript"></script>
        <script src="generated-js/org/stjs/hello/HelloWorld.js" type="text/javascript"></script>
  11. Add an (almost) empty WEB-INF/web.xml file in src/main/webapp to allow maven build
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns=""
  12. Build (* see generated JavaScript below) and deploy it to your application server (or use the Jetty Maven plugin ** to start it directly from the command line)
  13. Browse the page and click on the "Go" button

*) The generated JavaScript code:

var HelloWorld = function(){};
stjs.extend(HelloWorld, null, [], function(constructor, prototype){
    constructor.main = function(args) {
        window.onload = function(ev) {
            var form = window.document.forms[0];
            var button = form.elements["say"];
            var text = form.elements["to"];
            button.onclick = function(ev) {
                alert("Hello " + text.value);
                return true;
}, {});

if (!stjs.mainCallDisabled) HelloWorld.main();

**) Jetty configuration:

        <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">
Fork me on GitHub