Detalhes do pacote

java-caller

nvuillam105.9kMIT4.2.1

Library to easily call java from node sources. Automatically installs java if not present

java, caller, classpath, jar

readme (leia-me)

Java Caller for Node.js

Version Downloads/week Downloads/total Generated by github-dependents-info CircleCI Mega-Linter codecov GitHub contributors GitHub stars License PRs Welcome

Lightweight cross-platform javascript module to easily call java commands from Node.js sources.

  • Automatically installs required Java version if not present on the system
  • Compliant with JDK & JRE from 8 to 21
  • Uses node spawn method to perform the call

There are two ways to use java-caller:

  • module: Manually call JavaCaller in your custom JS/TS code (example project)
  • CLI: Just define a java-caller-config.json and you can deliver your java executables as your own NPM packages ! (example project, which can be used as starter kit)

Installation

npm install java-caller --save

Usage

const JavaCaller = require('java-caller');
const java = new JavaCaller(JAVA_CALLER_OPTIONS);
const {status, stdout, stderr} = await java.run(JAVA_ARGUMENTS,JAVA_CALLER_RUN_OPTIONS);

JAVA_CALLER_OPTIONS

Parameter Description Default value Example
jar Path to executable jar file "myfolder/myjar.jar"
classPath If jar parameter is not set, classpath to use
Use : as separator (it will be converted if runned on Windows), or use a string array.
. (current folder) "java/myJar.jar:java/myOtherJar.jar"
useAbsoluteClassPaths Set to true if classpaths should not be based on the rootPath false true
mainClass If classPath set, main class to call "com.example.MyClass"
rootPath If classPath elements are not relative to the current folder, you can define a root path.
You may use __dirname if you classes / jars are in your module folder
. (current folder) "/home/my/folder/containing/jars"
minimumJavaVersion Minimum java version to be used to call java command.
If the java version found on machine is lower, java-caller will try to install and use the appropriate one
8 11
maximumJavaVersion Maximum java version to be used to call java command.
If the java version found on machine is upper, java-caller will try to install and use the appropriate one
Can be equal to minimumJavaVersion
10
javaType jre or jdk (if not defined and installation is required, jre will be installed) "jre"
additionalJavaArgs Additional parameters for JVM that will be added in every JavaCaller instance runs ["-Xms256m","-Xmx2048m"]
javaExecutable You can force to use a defined java executable, instead of letting java-caller find/install one. Can also be defined with env var JAVA_CALLER_JAVA_EXECUTABLE "/home/some-java-version/bin/java.exe"

JAVA_ARGUMENTS

The list of arguments can contain both arguments types together:

  • Java arguments (-X*, -D*). ex: "-Xms256m", "-Xmx2048m"
  • Main class arguments (sent to public static void main method). ex: "--someflag" , "--someflagwithvalue myVal" , "-c"

Example: ["-Xms256m", "--someflagwithvalue myVal", "-c"]

JAVA_CALLER_RUN_OPTIONS

Parameter Description Default Example
detached If set to true, node will node wait for the java command to be completed.
In that case, childJavaProcess property will be returned, but stdout and stderr may be empty, except if an error is triggered at command execution
false true
stdoutEncoding Adds control on spawn process stdout utf8 ucs2
waitForErrorMs If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run 500 2000
cwd You can override cwd of spawn called by JavaCaller runner process.cwd() some/other/cwd/folder
javaArgs List of arguments for JVM only, not the JAR or the class [] ['--add-opens=java.base/java.lang=ALL-UNNAMED']
windowsVerbatimArguments No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. true false
windowless%20method.-,javaw,information%20if%20a%20launch%20fails.) If windowless is true, JavaCaller calls javaw instead of java to not create any windows, useful when using detached on Windows. Ignored on Unix. false true

Examples

Call a class located in classpath

const java = new JavaCaller({
    classPath: 'test/java/dist',
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();

Call a class with multiple folders in the classPath

const java = new JavaCaller({
    classPath: ['C:\\pathA\\test\\java\\dist', 'C:\\pathB\\test\\java\\dist'],
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();

Call a class located in classpath with java and custom arguments

const java = new JavaCaller({
    classPath: 'test/java/dist',
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['-Xms256m', '-Xmx2048m', '--customarg nico']);

Call a class in jar located in classpath

const java = new JavaCaller({
    classPath: 'test/java/jar/JavaCallerTester.jar',
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run();

Call a runnable jar

const java = new JavaCaller({
    jar: 'test/java/jar/JavaCallerTesterRunnable.jar',
});
const { status, stdout, stderr } = await java.run();

Call a detached java process

const java = new JavaCaller({
    classPath: 'test/java/dist',
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr, childJavaProcess } = await java.run(['--sleep'], { detached: true });

// Kill later the java process if necessary
childJavaProcess.kill('SIGINT');

Call a windowless java process

const java = new JavaCaller({
    classPath: 'test/java/dist',
    mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['--sleep'], { windowless: true });

You can see more examples in test methods

TROUBLESHOOTING

Set environment variable DEBUG=java-caller before calling your code using java-caller module, and you will see the java commands executed.

Example debug log:

java-caller Found Java version 1.80131 +1s
java-caller Java command: java -Xms256m -Xmx2048m -cp C:\Work\gitPerso\node-java-caller\test\java\dist com.nvuillam.javacaller.JavaCallerTester -customarg nico +1ms

CONTRIBUTE

Contributions are very welcome !

Please follow Contribution instructions

RELEASE NOTES

See complete CHANGELOG

changelog (log de mudanças)

Changelog

Unreleased

[4.2.1] 2025-05-25

  • Fix to allow absolute path to JAR file
  • Upgrade dependencies

[4.2.0] 2025-02-23

  • Upgrade to njre v1.4.2
  • Upgrade dependencies

[4.1.1] 2024-08-30

  • Upgrade to njre v1.4.0
  • Fix issue when package called from ES Module by using njre installPath option

[4.1.0] 2024-08-20

  • Upgrade to MegaLinter v8
  • Upgrade npm dependencies, including base package njre to 1.3.0

[4.0.0] 2024-05-08

  • When java used has been installed by JavaCaller, use full java executable path to perform the calls
    • Before 4.00: Update PATH + java -cp /home/circleci/project/test/java/dist com.nvuillam.javacaller.JavaCallerTester
    • Since 4.0.0: Update PATH + /home/circleci/.java-caller/jre/jdk-20.0.2+9/bin/java -cp /home/circleci/project/test/java/dist com.nvuillam.javacaller.JavaCallerTester
    • For example handles issue where Java 21 is installed and you need to run Java 17 with JavaCaller
  • Refactor CI/CD
    • Add additional tests in GitHub Actions
    • Test in more contexts (Mac, Java 21...)
  • Java 8 and 14 on Mac are not supported: Set default minimum java version to 11 on Mac

[3.3.1] 2024-04-28

  • Upgrade tar dependency to avoid CVE

[3.3.0] 2024-01-29

[3.2.0] 2023-11-26

  • Upgrade njre to v1.1.0 (now handles Mac M1)

[3.1.2] 2023-11-25

  • Add support for configuring windowsVerbatimArguments on run to make it easier to create cross platform compatible code.

[3.1.1] 2023-11-19

  • fix couple of issues in the rule used to detect if desired java version is installed or not, by @djukxe in #46

[3.1.0] 2023-11-18

  • Use semver module to check found java version instead of custom code
  • Add java 17 to test cases
  • Automate and secure releases using GitHub Actions
  • Inclusivity: Rename git branch master into main

[3.0.0] 2023-09-19

  • Upgrade njre to 1.0.0 (Allowing to install until Java 20)
  • Upgrade dependencies

[2.7.0] 2022-11-16

  • add stdoutEncoding option (default utf8) (#26, by danunafig)

[2.6.0] 2022-09-11

  • Fix override of java executable on Linux & Mac environments (#23)
  • Allow to use JAVA_CALLER_JAVA_EXECUTABLE environment variable to force javaExecutable option
  • Fix npm audit issues
  • Upgrade dependencies
    • debug
    • eslint
    • fs-extra
    • mocha
    • njre
    • nyc
    • which

[2.5.0] 2022-08-10

[2.4.0] 2020-07-19

  • Fix additionalJavaArgs issue #13

[2.3.0] 2020-09-05

  • Support absolute paths in classpath with argument useAbsoluteClassPaths and classPath as array of strings (#12, by Dan Gowans)

[2.2.3] 2020-09-05

[2.2.0] 2020-08-29

  • Fix CLASSPATH on windows in case there are spaces in paths
  • Update License to MIT

[2.1.0] 2020-08-12

  • Allow to use java-caller to build your own CLI embedding java sources
  • Example projects using module and CLI

[2.0.0] 2020-08-11

  • Big refactoring to simplify and enhance performances of code checking/installing java version
  • Replace use of deprecated package node-jre by njre
  • Compliance with JDK & JRE from 8 to 14 (AdoptOpenJdk releases)

[1.1.0] 2020-08-10

  • Return javaChildProcess when detached is true, so it can be used to be killed later

[1.0.0] 2020-08-10

  • Initial version