ESJava

raw JSON →
0.0.8 verified Fri May 01 auth: no javascript abandoned

ESJava (v0.0.8) is an experimental transpiler that converts Java source code to ES6 (ES2015) JavaScript. It targets ES6 features such as classes, arrow functions, and static getters. Release cadence is low; the project has not seen updates since 2016. Compared to other Java-to-JS tools like GWT or J2CL, ESJava is a lightweight, single-pass transpiler focused on syntactic translation without runtime libraries. It is primarily a proof-of-concept for converting small Java programs, with limited support for Java standard library or advanced language features.

error Error: Cannot find module 'esjava'
cause Package not installed or installed but using CommonJS require with an ESM-only package.
fix
Install the package: npm install esjava. If using require, switch to import or use dynamic import().
error SyntaxError: Unexpected token 'export'
cause Trying to require an ES module with CommonJS require.
fix
Use import instead of require, or set type: module in package.json.
deprecated Package has not been updated since 2016 and is effectively abandoned.
fix Consider using modern alternatives like J2CL or TeaVM for production use.
gotcha Only supports a subset of Java 8 features; many standard library classes (e.g., java.util) are not transpiled and will cause runtime errors.
fix Avoid using Java standard library classes that are not ES6 equivalents; manually implement or polyfill missing functionality.
gotcha Static final fields are transpiled to ES6 getters, which are read-only but can be overridden in subclasses, differing from Java's final behavior.
fix Be aware that static final fields are not truly immutable across subclasses; use Object.defineProperty for stricter immutability if needed.
deprecated The CLI tool uses an older version of commander; command-line usage may not work with recent Node.js versions.
fix Use the programmatic API (import { compile } from 'esjava') instead of the CLI.
npm install esjava
yarn add esjava
pnpm add esjava

Demonstrates converting a simple Java class with a main method to ES6 JavaScript using the compile function.

import { compile } from 'esjava';

const javaCode = `
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
`;

const jsCode = compile(javaCode);
console.log(jsCode);
// Output:
// 'use strict';
// class Hello {
//   static main(args) {
//     console.log("Hello, world!");
//   }
// }