36

J2CL: A Java to JavaScript Transpiler

 5 years ago
source link: https://www.tuicool.com/articles/uMfqaqI
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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." It attempts to solve a different problem than similar Java-to-Javascript frameworks such as GWT. Likewise, J2CL is not meant to compete with or replace existing JavaScript frameworks, 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK