package
Bridge API to connect with existing Java APIs.
npm install javaGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Bridge API to connect with existing Java APIs.
Google Groups Discussion Forum
$ npm install javaNotes:
node ./scripts/findJavaHome.js in the node-java directory to see the full failure message.sudo apt install make g++EACCES user nobody does not have permission to access the dev dir /root/.cache/node-gyp/10.16.0, then just run: npm i -g java --unsafe-permFor 64 bit installs with 32 bit node:
If you get ENOENT errors looking for <nodepath>\node_modules\node-gyp\.., ensure you have node-gyp installed as a global nodule:
npm install -g node-gypIf you get D9025 warnings and C1083 errors when looking for .sln or .h files, be sure you've got the node-gyp's dependencies, as explained here.
Alternatively, Windows users can easily install all required tools by running the following command in PowerShell as administrator. For more information see windows-build-tools project page:
npm install --global --production windows-build-toolsGYP_DEFINES="armv7=0" CCFLAGS='-march=armv6' CXXFLAGS='-march=armv6' npm install java./scripts/compile-java.sh
node-gyp configure build
npm testNOTE: You will need node-gyp installed using "npm install -g node-gyp"
On Raspian you might need a:
Some issues with the OpenSDK7 so take the Oracle version for compiling.
If you want to play with node-java but don't want to setup the build environment you can run it in docker.
docker run -it joeferner/node-java bashThen inside the docker container create a directory and run
npm install --unsafe-perm javaThen create a file called test.js with the following contents
const java = require('java');
const javaLangSystem = java.import('java.lang.System');
javaLangSystem.out.printlnSync('Hello World');Then run
node test.jsnpm install -g nw-gyp
npm install java
cd node_modules/java
nw-gyp configure --target=0.10.5
nw-gyp buildSee testIntegration/webkit for a working example
When using node-java in existing maven projects, all the dependencies and the class files of the project have to be pushed to the classpath.
One possible solution would be:
Issue the command:
mvn dependency:copy-dependencies
Then create the following module javaInit:
"use strict";
const fs = require("fs");
const java = require("java");
const baseDir = "./target/dependency";
const dependencies = fs.readdirSync(baseDir);
dependencies.forEach(function(dependency){
java.classpath.push(baseDir + "/" + dependency);
})
java.classpath.push("./target/classes");
java.classpath.push("./target/test-classes");
exports.getJavaInstance = function() {
return java;
}and then in the consuming class write:
const javaInit = require('./javaInit');
const java = javaInit.getJavaInstance();
//your code goes hereconst java = require("java");
java.classpath.push("commons-lang3-3.19.0.jar");
java.classpath.push("commons-io.jar");
const list1 = java.newInstanceSync("java.util.ArrayList");
console.log(list1.sizeSync()); // 0
list1.addSync('item1');
console.log(list1.sizeSync()); // 1
java.newInstance("java.util.ArrayList", function(err, list2) {
list2.addSync("item1");
list2.addSync("item2");
console.log(list2.toStringSync()); // [item1, item2]
});
const ArrayList = java.import('java.util.ArrayList');
const list3 = new ArrayList();
list3.addSync('item1');
list3.equalsSync(list1); // trueconst charArray = java.newArray("char", "hello world\n".split(''));const byteArray = java.newArray(
"byte",
"hello world\n"
.split('')
.map(function(c) { return java.newByte(String.prototype.charCodeAt(c)); }));JavaScript only supports 32-bit integers. Because of this java longs must be treated specially. When getting a long result the value may be truncated. If you need the original value there is a property off of the result called "longValue" which contains the un-truncated value as a string. If you are calling a method that takes a long you must create it using java.newInstance.
const javaLong = java.newInstanceSync("java.lang.Long", 5);
console.log('Possibly truncated long value: ' + javaLong);
console.log('Original long value (as a string): ' + javaLong.longValue);
java.callStaticMethodSync("Test", "staticMethodThatTakesALong", javaLong);Exceptions from calling methods either caught using JavaScript try/catch block or passed to a callback as the first parameter may have a property named "cause" which has a reference to the Java Exception object which caused the error.
try {
java.methodThatThrowsExceptionSync();
} catch(ex) {
console.log(ex.cause.getMessageSync());
}Methods accepting varargs of a generic type are also problematic. You will need to fall back to using java.newArray(). See Issue #285.
With v0.5.1 a new API is available to make it easier for a complex application to have full control over JVM creation. In particular, it is now easier to compose an application from several modules, each of which must add to the Java classpath and possibly do other operations just before or just after the JVM has been created. See the methods ensureJvm and registerClient. See also several of the tests in the testAsyncOptions directory.
See Async Options for details.
'$' (eg. com.nearinfinty.MyClass$NestedClass).Example
const result = java.callStaticMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
java.callStaticMethod("com.nearinfinty.MyClass", "doSomething", 42, "test", function(err, results) {
if(err) { console.error(err); return; }
// results from doSomething
});Example
const instance = java.newInstanceSync("com.nearinfinty.MyClass");
const result = java.callMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
java.callMethod(instance, "doSomething", 42, "test", function(err, results) {
if(err) { console.error(err); return; }
// results from doSomething
});Estimated unpacked size across 31 packages.
20.0 MB
Includes dependencies.
4 direct dependencies.