node-java icon indicating copy to clipboard operation
node-java copied to clipboard

Not able to execute node-java functions

Open JakePrasad opened this issue 13 years ago • 6 comments

I'm currently using Mac OS X 10.9 Mavericks and was trying out the quick examples code in the read me:

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

...

At the start of the program everything works fine but I'm not able to execute any functions java related or node related after I hit the line:

var list = java.newInstanceSync("java.util.ArrayList");

So I think somehow the java variable is not initialized properly. I tried replacing the newInstanceSync function with a regular newInstance function with a callback but got the same results. Same problem with declaring a new char array.

I'm able to log the java variable before I hit the newInstanceSync problem and this is what I am getting java to be defined as:

{ classpath: [ '/Users/jakeprasad/node_modules/java/commons-lang3-node-java.jar', '/Users/jakeprasad/node_modules/java/src-java', 'commons-lang3-3.1.jar', 'commons-io.jar' ], options: [], nativeBindingLocation: '/Users/jakeprasad/node_modules/java/build/Release/nodejavabridge_bindings.node', import: [Function] }

I also tried logging java.newInstanceSync and I am getting the result

[Function: newInstanceSync]

so there definitely is a function named newInstanceSync. I'm not sure in which file the function is defined, though, or I would try to go about trying to debug it.

Thanks, Jake

JakePrasad avatar Nov 16 '13 03:11 JakePrasad

Can you try going to the node-java directory and run "npm test" this will run all the unit test. Also what happens when you run var list = java.newInstanceSync("java.util.ArrayList"); can you run console.log(list.getClassSync().getNameSync()); after that to see what happens?

joeferner avatar Nov 16 '13 05:11 joeferner

Hi Joe,

Thanks for the quick reply.

I tried running the unit tests and all 137 passed which was surprising.

However, on closer inspection of the logs from the tests, I noticed that a couple methods had "bad method name" and that there were some other weird logs as well like "bad type" and "method overload":

awt-test
java.awt.headless = true
✔ AWT - calling AWT method (see issue 1)
dynamicProxy-test
✔ Dynamic Proxy - 0 Arguments
✔ Dynamic Proxy - 1 Arguments
✔ Dynamic Proxy - 1 Arguments with return data
✔ Dynamic Proxy - thread
importClass-test
✔ Import Class - import
instanceof-test
✔ instanceOf - working
✔ instanceOf - non-java object
✔ instanceOf - bad type
java-callStaticMethod-test
✔ Java - Call Static Method - callStaticMethod
✔ Java - Call Static Method - callStaticMethodSync
✔ Java - Call Static Method - callStaticMethod with args
✔ Java - Call Static Method - callStaticMethodSync with args
✔ Java - Call Static Method - callStaticMethod bad class name
✔ Java - Call Static Method - callStaticMethodSync bad class name
✔ Java - Call Static Method - callStaticMethod bad arg types
✔ Java - Call Static Method - callStaticMethodSync bad arg types
✔ Java - Call Static Method - callStaticMethod bad number of args
✔ Java - Call Static Method - callStaticMethodSync bad number of args
✔ Java - Call Static Method - callStaticMethod bad method name
✔ Java - Call Static Method - callStaticMethodSync bad method name
✔ Java - Call Static Method - callStaticMethod exception thrown from method
✔ Java - Call Static Method - callStaticMethodSync exception thrown from method
✔ Java - Call Static Method - callMethodSync exception thrown from method
✔ Java - Call Static Method - char array
✔ Java - Call Static Method - java.lang.Long addition
✔ Java - Call Static Method - java.lang.Long calls (java Long)
✔ Java - Call Static Method - Call method that returns a long
java-newInstance-test
✔ Java - New Instance - newInstance
✔ Java - New Instance - newInstanceSync
✔ Java - New Instance - newInstance with args
✔ Java - New Instance - newInstanceSync with args
✔ Java - New Instance - newInstance bad class name
✔ Java - New Instance - newInstanceSync bad class name
✔ Java - New Instance - newInstance bad arg types
✔ Java - New Instance - newInstanceSync bad arg types
✔ Java - New Instance - newInstance bad number of args
✔ Java - New Instance - newInstanceSync bad number of args
✔ Java - New Instance - newInstance exception thrown from constructor
✔ Java - New Instance - newInstanceSync exception thrown from constructor
java-staticField-test
✔ Java - Static Field - getStaticFieldValue int
✔ Java - Static Field - setStaticFieldValue int
✔ Java - Static Field - getStaticFieldValue double
✔ Java - Static Field - setStaticFieldValue double
✔ Java - Static Field - setStaticFieldValue double (set int)
javaObject-test
✔ Java Object - field
simple-test
org.apache.commons.lang3.ObjectUtils.toString: test
✔ Simple - test classpath commons lang
classpath [ '/Users/jakeprasad/node_modules/java/commons-lang3-node-java.jar',
  '/Users/jakeprasad/node_modules/java/src-java',
  'test/',
  'test/commons-lang3-3.1.jar' ]
✔ Simple - test adding to classpath after other calls are made
options [ '-Djava.awt.headless=true' ]
✔ Simple - test changing options after other calls are made
nativeBindingLocation /Users/jakeprasad/node_modules/java/build/Release/nodejavabridge_bindings.node
✔ Simple - test changing nativeBindingLocation after other calls are made
currentTimeMillis: { longValue: '1384582728070' }
✔ Simple - test static calls
os.version: 10.9
✔ Simple - test static calls single argument
✔ Simple - test method does not exists (sync)
✔ Simple - test method does not exists (async)
✔ Simple - create an instance of a class and call methods (getName) (async)
✔ Simple - create an instance of a class and call methods (getName) (sync)
✔ Simple - create an instance of a class and call methods (size) (async)
bufferedReader.readLineSync hello world
✔ Simple - passing objects to methods
[ 1, 2, 3, 4, 5 ]
✔ Simple - method returning an array of ints sync
[ 1, 2, 3, 4, 5 ]
✔ Simple - method returning an array of bytes sync
[ true, true, false, true, false ]
✔ Simple - method returning an array of bools sync
[ 1, 2, 3, 4, 5 ]
✔ Simple - method returning an array of doubles sync
[ 1, 2, 3, 4, 5 ]
✔ Simple - method returning an array of floats sync
[ '9223372036854775807', '-9223372036854775808', '3', '4', '5' ]
✔ Simple - method returning an array of longs sync
1
✔ Simple - method taking a byte
1
✔ Simple - method taking a short
3.14
✔ Simple - method taking a double
3.140000104904175
✔ Simple - method taking a float
1
✔ Simple - method taking a long
97
✔ Simple - method taking a char (number)
97
✔ Simple - method taking a char (string)
✔ Simple - new boolean array object
✔ Simple - new boolean array
✔ Simple - new short array objects
✔ Simple - new short array
utils-types-test
✔ Utils - Types - Array of Objects
✔ Utils - Types - Static Method Overload
✔ Utils - Types - Method Overload
OK: 137 assertions (484ms)

However, the unit tests still appear to pass (although the check marks are the normal console font color not green) so I'm not sure if the logs for the unit tests indicate an error.

Also, I'm not able to get any code to run after the line

list = java.newInstanceSync("java.util.ArrayList");

not even

console.log("Test")
or
console.log(list.getClassSync().getNameSync());

Thanks, Jake

JakePrasad avatar Nov 16 '13 06:11 JakePrasad

Those bad type, etc messages are all expected. They are just testing various scenarios.

You just get nothing after the java call? Does the node process exit?

Can you post the whole file you are trying to run.

joeferner avatar Nov 17 '13 12:11 joeferner

Yep, I get nothing after the java call but the node process doesn't exit. I was trying to figure out what else could be the problem and the only other possibility I was able to come up with was that I was using one of the node frameworks, meteor.js. However, I am able to run other npm packages fine.

I tried checking if it really was Meteor that was causing the problem by executing the quick start code in a regular node file. Although I was able to log to the console after the create new Instance function, the list variable returned an empty object even after running the

list.addSync("item1");
code.

This is the code I have for the regular node file(Not using Meteor.js):

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");
  console.log("Inside the callback");
  console.log(list);
  console.log(list[0]);
  console.log("Outside the callback");
})
list.getClassSync().getNameSync();
console.log(list);
console.log(list[0]);
var ArrayList = java.import('java.util.ArrayList');
var list = new ArrayList();
list.addSync('item1');
console.log(list.getClassSync().getNameSync());
console.log(list);
console.log("------Java Long-----");
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);

and this is what is logged to the console when I run the file:

{}
undefined
java.util.ArrayList
{}
------Java Long-----
Possibly truncated long value: 5
Original long value (as a string): 5
Inside the callback
{}
undefined
Outside the callback

and this is what my Meteor file looks like (The only difference is I use Npm.require and not require and I am using coffee script):

java = Npm.require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
exec = Npm.require("child_process").exec
console.log(java)
list = java.newInstanceSync("java.util.ArrayList");
console.log("There")
console.log(list.getClassSync().getNameSync());

and this is the output:

 
{ classpath:
[ '/Users/jakeprasad/node_modules/java/commons-lang3-node-java.jar',
 '/Users/jakeprasad/node_modules/java/src-java',
 'commons-lang3-3.1.jar',
 'commons-io.jar' ],
options: [],
nativeBindingLocation: '/Users/jakeprasad/node_modules/java/build/Release/nodejavabridge_bindings.node',
import: [Function] 
}

If the only problem is the interaction between this package and Meteor, I'll post the error message on the Meteor github page, but I don't think that's the problem since even with a regular node file the list variable seems to be empty after the addSync methods.

JakePrasad avatar Nov 17 '13 19:11 JakePrasad

So when you use this, you have to use it the way Java expects it to work. An ArrayList doesn't work like an array in JS. You have to do list.getSync(0) instead of list[0].

Here's your code, javafied:

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

console.info("List is", list);

java.newInstance("java.util.ArrayList", function(err, list) {
  list.addSync("item1");
  list.addSync("item2");
  console.log("Inside the callback");

  // To see the contents, you need to call toArray()
  console.log(list.toArraySync());
  console.log(list.getSync(0));
  console.log("Outside the callback");
});

console.log(list.getClassSync().getNameSync());
console.log(list.toArraySync());

// This will crash as there's nothing at that index
// console.log(list.getSync(0));

var ArrayList = java.import('java.util.ArrayList');
var list = new ArrayList();
list.addSync('item1');
console.log(list.getClassSync().getNameSync());
console.log(list.toArraySync());


console.log("------Java Long-----");

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

Here's the output:

List is {}
java.util.ArrayList
[]
java.util.ArrayList
[ 'item1' ]
------Java Long-----
Possibly truncated long value: 5
Original long value (as a string): 5
Inside the callback
[ 'item1', 'item2' ]
item1
Outside the callback

And just so you can see what happens when you do something java doesn't like, here's what happens when you get on a bad index:

List is {}
java.util.ArrayList
[]

/Users/vwhitco/Projects/node-java/test.js:24
console.log(list.getSync(0));
                 ^
Error: Error running instance method
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

    at Object.<anonymous> (/Users/vwhitco/Projects/node-java/test.js:24:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

SinisterMinister avatar Nov 20 '13 00:11 SinisterMinister

Thanks. That got my regular node program working :) Unfortunately, it doesn't solve the problem about why the code doesn't log anything to the console when I run it using meteor.js. However, it turns out I don't need to run a java application after all. So this issue can be closed for now since the issue seems to be caused by meteor and not this package.

JakePrasad avatar Nov 22 '13 01:11 JakePrasad