BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News J2CL: A Java-to-JavaScript Transpiler

J2CL: A Java-to-JavaScript Transpiler

Leia em Português

This item in japanese

Lire ce contenu en français

Designed, developed, and open-sourced by Google, J2CL is a source-to-source compiler that converts Java to Javascript. J2CL developers say the compiler "enables seamless use of Java in your JavaScript applications."

J2CL attempts to solve a different problem than similar Java-to-Javascript frameworks such as GWT, and it is not meant to compete with or replace existing JavaScript frameworks. Instead, J2CL is about interoperability and cross-platform code reuse. According to the developers, "[y]ou can use J2CL to just make some Java code accessible from JavaScript or go all the way to create a whole application with it; whatever best suits your needs."

The tool was developed with flexibility in mind and can be applied in a number of different ways. One such way is to ensure complex logic running on multiple platforms implemented in different technologies, can be shared across platforms. For example, Thomas Deegan, a previous contributor to the J2CL project, says "the code powering Google Docs front end needs to be written for web ... J2CL does the transpilation of all document manipulation and rendering logic from Java -> JS". Another use might be reusing a library written in Java in a Node.js application.

J2CL can translate most existing Java code from source but not all Java APIs are supported, such as the Java reflection API. By default, translated code is not public and a number of Java annotations from JsInterop can be used to identify what classes, methods, instance variables, or other code should be exposed. The project includes a very simple "hello world" example to demonstrate usage. A slightly modified excerpt of that example is included below showing an annotated Java class and how it can be used in JavaScript.

The following Java class, ran through J2CL:

package com.acme

import jsinterop.annotations.JsType;

@JsType
public class HelloWorld {

  public static String getHelloWorld() {
    return "Hello from Java!";
  }

}

can be used in JavaScript like below.

const HelloWorld = goog.require('com.acme');
console.log(HelloWorld.getHelloWorld());

The developers claim it is production ready and serves as "the underlying technology of the most advanced GSuite apps developed by Google including GMail, Inbox, Docs, Slides and Calendar."

The project was open sourced late last year, but talks of the project go as far back as 2014. Originally, J2CL was associated with the GWT project. However, in early 2015, the developers determined that the goals of the project could not be accomplished as part of the GWT project and decided to break it out into its own project. Integration with existing tools is not yet available and support for Windows is currently limited.

For additional information, visit the project's Github page or getting started guide.

Rate this Article

Adoption
Style

BT