使用桥接 API 连接到现有的 Java API。
Bridge API to connect with existing Java APIs.
Google Groups Discussion Forum
$ npm install java
Notes:
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-gyp
If 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-tools
GYP_DEFINES="armv7=0" CCFLAGS='-march=armv6' CXXFLAGS='-march=armv6' npm install java
./scripts/compile-java.sh
node-gyp configure build
npm test
NOTE: 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 bash
Then inside the docker container create a directory and run
npm install --unsafe-perm java
Then 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.js
npm install -g nw-gyp
npm install java
cd node_modules/java
nw-gyp configure --target=0.10.5
nw-gyp build
See 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 here
…
const 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());
}
As of release 0.4.5 it became possible to create async methods that return promises by setting the asyncOptions property of the java object. With release 0.4.7 this feature is extended to allow changing the suffix assigned for sync and async method variants, and to further configure this module to optionally omit generation of any of these variants.
Example:
…
asyncSuffix, syncSuffix, promiseSuffix). In the example above, the application is configured to omit the method variants using node-style async callback functions.asyncSuffix or syncSuffix can be the empty string. If you want the defacto standard behavior for no suffix on async methods, you must provide an empty string for asyncSuffix.testHelpers.js for more information.java.newInstancePromise, java.callMethodPromise, and java.callStaticMethodPromise are not available until the JVM has been created. You may need to call some other java method such as java.import() to finalize java initialization, or even better, the function java.ensureJvm().newInstance, callMethod, and callStaticMethod.These methods come in both async and sync variants. If you provide the promiseSuffix attributes in asyncOptions then you'll also get the Promises/A+ variant for these three functions. However, if you change the defacto conventions for the syncSuffix (i.e. 'Sync') and/or asyncSuffix (i.e. '') it will not affect the naming for these three functions. I.e. no matter what you specify in asyncOptions, the async variants are named newInstance, callMethod, and callStaticMethod, and the sync variants are named newInstanceSync, callMethodSync, and callStaticMethodSync.
With v0.5.0 node-java now supports methods with variadic arguments (varargs). Prior to v0.5.0, a JavaScript call to a Java varargs method had to construct an array of the variadic arguments using java.newArray(). With v0.5.0 JavaScript applications can simply use the variadic style.
In most cases it is still acceptable to use java.newArray(). But it is now possible to pass a plain JavaScript array, or use the variadic style. For example, consider these snippets from the unit test file test/varargs-test.js:
test.equal(Test.staticVarargsSync(5, 'a', 'b', 'c'), '5abc');
test.equal(Test.staticVarargsSync(5, ['a', 'b', 'c']), '5abc');
test.equal(Test.staticVarargsSync(5, java.newArray('java.lang.String', ['a', 'b', 'c'])), '5abc');
Note that when passing a JavaScript array (e.g. ['a', 'b', 'c']) for a varargs parameter, node-java must infer the Java type of the array. If all of the elements are of the same JavaScript primitive type (string in this example) then node-java will create a Java array of the corresponding type (e.g. java.lang.String). The Java types that node-java can infer are: java.lang.String, java.lang.Boolean, java.lang.Integer, java.lang.Long, and java.lang.Double. If an array has a mix of Integer, Long, and Double, then the inferred type will be java.lang.Number. Any other mix will result in an inferred type of java.lang.Object.
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.
<a name="javaClassp
暂无开放 Issues,或尚未同步最近议题。