j4ts
raw JSON → 0.7.2 verified Fri May 01 auth: no javascript
j4ts is a partial translation of the Java standard library (JRE) to JavaScript, forked from GWT's Java emulation and transpiled via JSweet. This NPM package (j4ts) provides the generated JavaScript and TypeScript definitions for use in JavaScript/TypeScript projects. Currently at version 0.7.2, it targets JSweet v2 and requires at least Java 8. It offers core Java collections, utilities, and NIO-like buffers. The package is ESM-compatible since v0.7.x. Compared to other Java-to-JS solutions (e.g., GWT, CheerpJ), j4ts is lightweight and designed for gradual migration of Java code to TypeScript. Release cadence is irregular, driven by j4ts upstream.
Common errors
error Cannot find module 'j4ts' or its corresponding type declarations. ↓
cause Package not installed or TypeScript cannot resolve types because the package lacks a 'types' field in package.json or @types/j4ts is missing.
fix
Run
npm install j4ts (or yarn add j4ts). Ensure tsconfig.json includes 'node_modules/@types' or the package's types are correctly configured. error Module not found: Can't resolve 'j4ts' in '/path/to/project' ↓
cause Webpack or bundler cannot resolve j4ts because it's not installed or the module resolution is misconfigured.
fix
Confirm j4ts is in your dependencies. If using Webpack, ensure 'resolve.modules' includes 'node_modules'. Also check that the package.json's 'main' field points to the correct file (j4ts.js).
Warnings
breaking Package is ESM-only since v0.7.x. Older CommonJS imports will fail. ↓
fix Update imports to use ES module syntax: import { ... } from 'j4ts'
breaking Versioning mismatch: NPM patch versions may not align with upstream j4ts releases. ↓
fix Check the exact j4ts version in the package's README or commit history.
deprecated JSweet v1 releases are obsolete. v0.3.x and earlier are for JSweet v1 and should not be used. ↓
fix Use j4ts >=0.5.0 with JSweet v2.
gotcha The package does not support all Java standard library classes. Only a subset is translated. ↓
fix Check the j4ts GitHub repository for supported classes before migrating.
Install
npm install j4ts yarn add j4ts pnpm add j4ts Imports
- ArrayList wrong
const ArrayList = require('j4ts').ArrayListcorrectimport { ArrayList } from 'j4ts' - HashMap wrong
import { HashMap } from 'j4ts/j4ts'correctimport { HashMap } from 'j4ts' - HashSet
import { HashSet } from 'j4ts' - java.util.ArrayList wrong
import { java } from 'j4ts'; const ArrayList = java.util.ArrayList;correctimport { ArrayList } from 'j4ts' - Arrays wrong
import * as j4ts from 'j4ts'; const Arrays = j4ts.Arrays;correctimport { Arrays } from 'j4ts'
Quickstart
import { ArrayList, HashMap, HashSet } from 'j4ts';
const list = new ArrayList<string>();
list.add('foo');
list.add('bar');
console.log(list.size()); // 2
const map = new HashMap<string, number>();
map.put('a', 1);
map.put('b', 2);
console.log(map.get('a')); // 1
const set = new HashSet<number>();
set.add(1);
set.add(2);
console.log(set.contains(1)); // true
console.log(set.size()); // 2