Bridge API to connect with existing Java APIs.
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.
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.
# Getting the Full Method Signature
Run javap -s -classpath <your-class-path> <your-class-name>
. Find the method name you are looking for. For example:
public int methodAmbiguous(java.lang.Double);
Signature: (Ljava/lang/Double;)I
The full method signature would be methodAmbiguous(Ljava/lang/Double;)I
.
If you have grep, a shortcut is javap -s -classpath . my.company.MyClass | grep -A1 myMethodName
.
Signal Handling
The JVM intercepts signals (Ctrl+C, etc.) before node/v8 gets to handle them. To fix this there are a couple options.
Signal Handling Option 1
One option to capture these events is to add the following flag:
java.options.push('-Xrs');
As man java
says, the -Xrs
flag will “reduce usage of operating-system signals by [the] Java virtual machine (JVM)”, to avoid issues when developing “applications that embed the JVM”.
Signal Handling Option 2
Hook into the runtime shutdown hook.
First create a java wrapper around the Runtime.addShutdownHook method to allow using a proxy object.
public class ShutdownHookHelper {
public static void setShutdownHook(final Runnable r) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
r.run();
}
});
}
}
Compile ShutdownHookHelper and then use it as follows.
var java = require('./');
java.classpath.push('.');
var ShutdownHookHelper = java.import('ShutdownHookHelper');
ShutdownHookHelper.setShutdownHookSync(java.newProxy('java.lang.Runnable', {
run: function () {
console.log("do shutdown stuff here instead.");
}
}));
Object lifetime
When you call a Java method through node-java, any arguments (V8/JavaScript objects) will be converted to Java objects on the v8 main thread via a call to v8ToJava (found in utils.cpp). The JavaScript object is not held on to and can be garbage collected by v8. If this is an async call, the reference count on the Java objects will be incremented. The Java method will be invoked in a node.js async thread (see uv_queue_work). When the method returns, the resulting object will be returned to the main v8 thread and converted to JavaScript objects via a call to javaToV8 and the Java object's reference count will then be decremented to allow for garbage collection. The resulting v8 object will then be returned to the callers callback function.
License
(The MIT License)
Copyright (c) 2012 Near Infinity Corporation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.