Create NPM wrappers for Java JARs with automatic JRE management.
This tool generates NPM packages that wrap Java JAR files, allowing them to be distributed and run via npm install without requiring users to manually install Java. If Java is not found on the system, the wrapper will automatically download and cache a JRE from Adoptium.
npm install -g npm-jar-wrapper-makerOr as a dev dependency in your Java project:
npm install --save-dev npm-jar-wrapper-makernpm-jar-wrapper-maker --initThis creates a wrapper-config.json file that you can customize.
npm-jar-wrapper-maker --config wrapper-config.json --output ./npm-packagenpm-jar-wrapper-maker \
--package-name @myorg/my-cli \
--cli-name my-cli \
--jar-file-name my-app.jar \
--java-version 17 \
--output ./npm-packageconst { generateWrapper, validateConfig } = require('npm-jar-wrapper-maker');
const config = {
packageName: '@myorg/my-cli',
version: '1.0.0',
description: 'My Java CLI tool',
cliName: 'my-cli',
jarFileName: 'my-app.jar',
targetJavaVersion: 17,
author: 'Your Name',
license: 'Apache-2.0',
};
// Validate configuration
const validation = validateConfig(config);
if (!validation.valid) {
console.error('Invalid config:', validation.errors);
process.exit(1);
}
// Generate the wrapper
const result = generateWrapper(config, './output-dir');
console.log('Generated files:', result.files);
console.log('Copy your JAR to:', result.jarPlaceholder);| Option | Type | Required | Default | Description |
|---|---|---|---|---|
packageName |
string | Yes | - | NPM package name (e.g., @myorg/my-cli) |
cliName |
string | Yes | - | CLI command name (e.g., my-cli) |
jarFileName |
string | Yes | - | JAR file name (e.g., my-app.jar) |
version |
string | No | 1.0.0 |
Package version |
description |
string | No | "" |
Package description |
author |
string | No | "" |
Package author |
license |
string | No | Apache-2.0 |
Package license |
targetJavaVersion |
number | No | 17 |
Target Java version (8+) |
javaOpts |
string[] | No | [] |
Additional JVM options |
enableNativeAccess |
boolean | No | false |
Enable Java native access |
enablePostinstall |
boolean | No | false |
Enable postinstall script |
postinstallCommand |
string | No | "" |
Custom postinstall command |
configDirName |
string | No | Same as cliName |
Config directory name for JRE cache |
repository |
object | No | {} |
Repository info for package.json |
homepage |
string | No | "" |
Homepage URL |
bugs |
object | No | {} |
Bugs URL |
keywords |
string[] | No | [] |
Additional keywords |
The generator creates the following files:
package.json- NPM package manifestindex.js- CLI entry point (with shebang)cli.js- Java runtime management and execution logic.gitignore- Git ignore rules.npmignore- NPM ignore rulesREADME.md- Basic documentationpostinstall.js- (optional) Post-install hook
-
When the CLI is executed, it first looks for Java:
- Checks
JAVA_HOMEenvironment variable - Checks for
javaon the system PATH - Checks for a previously downloaded JRE in
~/.config/<cli-name>/jre
- Checks
-
If no Java is found, it automatically:
- Fetches JRE metadata from Adoptium API
- Downloads the appropriate JRE for the user's platform and architecture
- Verifies the download checksum
- Extracts and caches the JRE locally
-
Executes the JAR file with the found/downloaded Java
You can integrate this tool into your Java build process. For example, in a Gradle project:
// build.gradle
task generateNpmWrapper(type: Exec) {
dependsOn bootJar
commandLine 'npx', 'npm-jar-wrapper-maker',
'--package-name', '@myorg/my-cli',
'--cli-name', 'my-cli',
'--jar-file-name', 'my-cli.jar',
'--output', "${buildDir}/npm-wrapper"
doLast {
copy {
from bootJar.archiveFile
into "${buildDir}/npm-wrapper"
rename { 'my-cli.jar' }
}
}
}Or with a configuration file:
task generateNpmWrapper(type: Exec) {
dependsOn bootJar
commandLine 'npx', 'npm-jar-wrapper-maker',
'--config', 'npm-wrapper-config.json',
'--output', "${buildDir}/npm-wrapper"
doLast {
copy {
from bootJar.archiveFile
into "${buildDir}/npm-wrapper"
rename { 'my-cli.jar' }
}
}
}The generated wrapper includes several security measures:
- Checksum verification: Downloaded JREs are verified against SHA256 checksums from Adoptium
- Path traversal protection: Archive extraction rejects paths that could escape the target directory (CVE-2020-12265 mitigation)
- HTTPS only: All downloads use HTTPS
The generated wrappers support:
- macOS (Intel and Apple Silicon)
- Linux (x64 and ARM64)
- Windows (x64)
Apache-2.0