Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
redseal-java
Advanced tools
Bridge API to connect with existing Java APIs. (RedSeal experimental fork)
Bridge API to connect with existing Java APIs.
** This is an experimental fork of joeferner/node-java. Unless you have good reason, you should use joeferner's fork. **
Google Groups Discussion Forum
###Other projects that might be helpful
$ npm install java
Notes:
node findJavaHome.js
in the node-java directory to see the full failure message.For 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.
GYP_DEFINES="armv7=0" CCFLAGS='-march=armv6' CXXFLAGS='-march=armv6' npm install java
./compile-java-code.sh
node-gyp configure build
npm test
NOTE: You will need node-gyp installed using "npm install -g node-gyp"
Manual compilation for Java 1.8 support requires additional steps:
./compile-java-code.sh
./compile-java8-code.sh
node-gyp configure build
npm test
npm test8
Java 1.8 language features can be used in Java classes only if a Java 1.8 JRE is available. The script compile-java8-code.sh is used only to compile java classes used in the 'test8' unit tests, but these classes are checked into the test8/ directory. Note that unit tests in the test8/ directory will pass (by design) if run against a Java 1.7 JRE, provided that a java.lang.UnsupportedClassVersionError is caught with the message 'Unsupported major.minor version 52.0' (the expected behavior when Java 1.8 language features are used in an older JRE).
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
var java = require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
var list = java.newInstanceSync("java.util.ArrayList");
java.newInstance("java.util.ArrayList", function(err, list) {
list.addSync("item1");
list.addSync("item2");
});
var ArrayList = java.import('java.util.ArrayList');
var list = new ArrayList();
list.addSync('item1');
var charArray = java.newArray("char", "hello world\n".split(''));
var byteArray = java.newArray(
"byte",
"hello world\n"
.split('')
.map(function(c) { return java.newByte(str.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.
var 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:
var java = require("java");
java.asyncOptions = {
asyncSuffix: undefined, // Don't generate node-style methods taking callbacks
syncSuffix: "" // Sync methods use the base name(!!)
promiseSuffix: "Promise", // Generate methods returning promises, using the suffix Promise.
promisify: require("when/node").lift
};
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
java.import("java.util.ArrayList"); // see NOTE below
java.newInstancePromise("java.util.ArrayList")
.then(function(list) { return list.addPromise("item1"); })
.then(function(list) { return list.addPromise("item2"); })
.catch(function(err) { /* handle error */ });
asyncSuffix
, syncSuffix
, promiseSuffix
). In the example above, the application is configured to omit the method variants using node-style async callback functions.asyncOptions.promiseSuffix
then you must also set asyncOptions.promisify
to a function that promisifies a node-style async function. I.e. the provided function must take as input a function whose last argument is a node callback function, and it must return an equivalent promise-returning function. Several Promises/A+ libraries provide such functions, but it may be necessary to provide a wrapper function. See testHelpers.js
for an example.asyncOptions.promisify
then you must provide a non-empty string for asyncOptions.promiseSuffix
.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 some other java method is called. You may need to call some other java method such as java.import()
to finalize java initialization.newInstance
, callMethod
, and callStaticMethod
.These methods come in both async and sync variants. If you provide the promisify
and 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
.
Array of paths or jars to pass to the creation of the JVM.
All items must be added to the classpath before calling any other node-java methods.
Example
java.classpath.push('commons.io.jar');
**java.options**
Array of options to pass to the creation of the JVM.
All items must be added to the options before calling any other node-java methods.
Example
java.options.push('-Djava.awt.headless=true');
java.options.push('-Xmx1024m');
**java.import(className)**
Loads the class given by className such that it acts and feels like a javascript object.
Arguments
Example
var Test = java.import('Test');
Test.someStaticMethodSync(5);
console.log(Test.someStaticField);
var value1 = Test.NestedEnum.Value1;
var test = new Test();
list.instanceMethodSync('item1');
**java.newInstance(className, [args...], callback)**
java.newInstanceSync(className, [args...]) : result
Creates an instance of the specified class. If you are using the sync method an exception will be throw if an error occures, otherwise it will be the first argument in the callback.
Arguments
Example
var list = java.newInstanceSync("java.util.ArrayList");
java.newInstance("java.util.ArrayList", function(err, list) {
if(err) { console.error(err); return; }
// new list
});
**java.instanceOf(javaObject, className)**
Determines of a javaObject is an instance of a class.
Arguments
Example
var obj = java.newInstanceSync("my.package.SubClass");
if(java.instanceOf(obj, "my.package.SuperClass")) {
console.log("obj is an instance of SuperClass");
}
**java.callStaticMethod(className, methodName, [args...], callback)**
java.callStaticMethodSync(className, methodName, [args...]) : result
Calls a static method on the specified class. If you are using the sync method an exception will be throw if an error occures, otherwise it will be the first argument in the callback.
Arguments
Example
var 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
});
**java.callMethod(instance, methodName, [args...], callback)**
java.callMethodSync(instance, methodName, [args...]) : result
Calls a method on the specified instance. If you are using the sync method an exception will be throw if an error occures, otherwise it will be the first argument in the callback.
Arguments
Example
var instance = java.newInstanceSync("com.nearinfinty.MyClass");
var result = java.callMethodSync("com.nearinfinty.MyClass", "doSomething", 42, "test");
java.callMethodSync(instance, "doSomething", 42, "test", function(err, results) {
if(err) { console.error(err); return; }
// results from doSomething
});
**java.getStaticFieldValue(className, fieldName)**
Gets a static field value from the specified class.
Arguments
Example
var data = java.getStaticFieldValue("com.nearinfinty.MyClass", "data");
**java.setStaticFieldValue(className, fieldName, newValue)**
Sets a static field value on the specified class.
Arguments
Example
java.setStaticFieldValue("com.nearinfinty.MyClass", "data", "Hello World");
**java.newArray(className, values[])**
Creates a new java array of type class.
Arguments
Example
var newArray = java.newArray("java.lang.String", ["item1", "item2", "item3"]);
**java.newByte(val)**
Creates a new java byte. This is needed because javascript does not have the concept of a byte.
Arguments
Example
var b = java.newByte(12);
**java.newShort(val)**
Creates a new java short. This is needed because javascript does not have the concept of a short.
Arguments
Example
var s = java.newShort(12);
**java.newLong(val)**
Creates a new java long. This is needed because javascript does not have the concept of a long.
Arguments
Example
var s = java.newLong(12);
**java.newChar(val)**
Creates a new java char. This is needed because javascript does not have the concept of a char.
Arguments
Example
var ch = java.newChar('a');
**java.newDouble(val)**
Creates a new java double. This is needed to force javascript's number to a double to call some methods.
Arguments
Example
var d = java.newDouble(3.14);
**java.newFloat(val)**
Creates a new java float. This is needed to force javascript's number to a float to call some methods.
Arguments
Example
var f = java.newFloat(3.14);
**java.newProxy(interfaceName, functions)**
Creates a new java Proxy for the given interface. Functions passed in will run on the v8 main thread and not a new thread.
The returned object has a method unref() which you can use to free the object for garbage collection.
Arguments
Example
var myProxy = java.newProxy('java.lang.Runnable', {
run: function () {
// This is actually run on the v8 thread and not the new java thread
console.log("hello from thread");
}
});
var thread = java.newInstanceSync("java.lang.Thread", myProxy);
thread.start();
## java object
**obj._methodName_([args...], callback)**
obj.methodNameSync([args...]) : result
Once you have a java object either by creating a new instance or as a result of a method call you can then call methods on that object. All public, non-static methods are exposed in synchronous and asynchronous flavors.
Arguments
Example
var list = java.newInstanceSync("java.util.ArrayList");
list.addSync("item1");
list.add("item2", function(err, result) {
if(err) { console.error(err); return; }
});
**obj._fieldName_ = val**
val = obj.fieldName
Once you have a java object either by creating a new instance or as a result of a method call you can get instance field values.
Example
var list = java.newInstanceSync("com.nearinfinty.MyClass");
list.data = "test";
var data = list.data;
# 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
.
The JVM intercepts signals (Ctrl+C, etc.) before node/v8 gets to handle them. To fix this there are a couple options.
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”.
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.");
}
}));
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.
Either postInstall.js didn't run or there was a problem detecting java. Try running postInstall.js manually.
(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.
FAQs
Bridge API to connect with existing Java APIs. (RedSeal experimental fork)
We found that redseal-java demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.