java-caller

1.0.0 • Public • Published

Java Caller for Node.js

Version Downloads/week Downloads/total CircleCI codecov GitHub contributors GitHub stars License PRs Welcome

Cross-platform JS module to easily call java commands from Node.js sources.

  • Automatically installs java (currently 1.8) if not present on the system
  • Uses node spawn method to perform the call

Installation

npm install java-caller --save

Usage

const JavaCaller = require('java-caller');
const java = new JavaCaller(JAVA_CALLER_OPTIONS);
const {status, stdout, stderr} = 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)
. (current folder) "java/myJar.jar:java/myOtherJar.jar"
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"
javaExecutable You can force to use a defined java executable, instead of letting java-caller find/install one "/home/some-java-version/bin/java.exe"
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
1.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
10
additionalJavaArgs Additional parameters for JVM ["-Xms256m","-Xmx2048m"]

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, stdout and stderr may be empty, except if an error is triggered at command execution
false true
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

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 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();

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

Package Sidebar

Install

npm i java-caller@1.0.0

Version

1.0.0

License

GPL-3.0-only

Unpacked Size

57 kB

Total Files

5

Last publish

Collaborators

  • nvuillam