Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

java

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

java

Bridge API to connect with existing Java APIs.

  • 0.2.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
18K
increased by3.96%
Maintainers
1
Weekly downloads
 
Created
Source

java

Bridge API to connect with existing Java APIs.

Installation Linux/Windows

$ export JAVA_HOME=/usr/local/share/jdk1.6.0_30
$ npm install java

Notes:

  • node-gyp requires python 2.x not python 3.x. See https://github.com/TooTallNate/node-gyp/issues/155 for more details.
  • If you see an error such as "No rule to make target '/opt/jdk1.7.0_09/jre/lib/amd64/server/libjvm.so', needed by 'build/depsVerified'. Stop." this means that your JAVA_HOME is not set correctly and you need to verify the location of libjvm.so or jvm.lib.
  • If you see an error such as "Error: The specified module could not be found. xxxxxx\node_modules\java\build\Release\nodejavabridge_bindings.node". Ensure the directory where jvm.dll exists is in your PATH. (e.g. C:\Program Files (x86)\Java\jdk1.6.0_18\jre\bin\client). This path cannot have quotes.

Installation Mac

$ npm install java

Installation ARM (Raspberry Pi)

GYP_DEFINES="armv7=0" CCFLAGS='-march=armv6' CXXFLAGS='-march=armv6' npm install java

Quick Examples

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

Create a char array

var charArray = java.newArray("char", "hello world\n".split(''));

Create a byte array

var byteArray = java.newArray(
  "byte",
  "hello world\n"
    .split('')
    .map(function(c) { return java.newByte(str.charCodeAt(c)); });

Using java.lang.Long and long

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

Release Notes

v0.2.0

Index

java

java objects

API Documentation

## java **java.import(className)**

Loads the class given by className such that it acts and feels like a javascript object.

Arguments

  • className - The name of the class to create. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)

Example

var Test = java.import('Test');
Test.someStaticMethodSync(5);
console.log(Test.someStaticField);

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

  • className - The name of the class to create. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • callback(err, item) - Callback to be called when the class is created.

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.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

  • className - The name of the class to call the method on. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • methodName - The name of the method to call.
  • callback(err, item) - Callback to be called when the class is created.

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.getStaticFieldValue(className, fieldName)**

Gets a static field value from the specified class.

Arguments

  • className - The name of the class to get the value from. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • fieldName - The name of the field to get the value from.

Example

var data = java.getStaticFieldValue("com.nearinfinty.MyClass", "data");
**java.setStaticFieldValue(className, fieldName, newValue)**

Sets a static field value on the specified class.

Arguments

  • className - The name of the class to set the value on. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • fieldName - The name of the field to set the value on.
  • newValue - The new value to assign to the field.

Example

java.getStaticFieldValue("com.nearinfinty.MyClass", "data", "Hello World");
**java.newArray(className, values[])**

Creates a new java array of type class.

Arguments

  • className - The name of the type of array elements. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • values - A javascript array of values to assign to the java array.

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

  • val - The value of the java byte.

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

  • val - The value of the java short.

Example

var s = java.newShort(12);
**java.newChar(val)**

Creates a new java char. This is needed because javascript does not have the concept of a char.

Arguments

  • val - The value of the java char.

Example

var ch = java.newChar('a');
**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 two methods ref() and unref() which you can use to maintain references to prevent premature garbage collection. You must call these methods to ensure the proxy stays around.

Arguments

  • interfaceName - The name of the interface to proxy. For subclasses seperate using a '$' (eg. com.nearinfinty.MyClass$SubClass)
  • functions - A hash of functions matching the function in the interface.

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

  • args - The arguments to pass to the method.
  • callback(err, item) - Callback to be called when the method has completed.

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;

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.

Keywords

FAQs

Package last updated on 24 Oct 2013

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc