1

I installed the node-java module (https://github.com/joeferner/node-java).

I have two files under the same folder named 'Code', one is test.js which has the following code:

let java= require('java');

let mySumTest= java.import('MySumClass');
console.log(mySumTest.sum(2, 5));

The other file is MySumClass.java:

public class MySumClass{
    public static int sum(int a, int b){
        return a+b;
    }
}

But when I try to run the test.js file (./Code> node test.js), the node.js command prompt says that:

Error: Could not create class MySumClass
java.lang.NoClassDefFoundError: MySumClass
Caused by: java.lang.ClassNotFoundException: MySumClass
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

It is possible that I misunderstood how to use the import function of the node-java module. Any help would be appreciated!

4 Answers 4

2

What I did was basically follow the OP's answer, but I had beforehand to create the .jar file. To do so, I just prepared my custom class in test.java:

import java.lang.System;

public class Test {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

And then

javac Test.java
jar cf test.jar Test.class

Now you should see the .jar created. From this point, you are able to access it from Javascript:

let java= require('java');
java.classpath.push('test.jar');

let test = java.import("Test");
test.main(null) // should print "Hello, World!"
Sign up to request clarification or add additional context in comments.

1 Comment

node-java does not seem to be actively maintained anymore - an alternative is java-bridge
1

After a lot of trying, this is what worked for me: (test.jar is in same directory as the other files)

let java= require('java');
java.classpath.push('test.jar');

let mySumTest= java.import("MySumClass");

/*
//this prints out undefined, don't know why
console.log(java.callStaticMethod("MySumClass", "sum", 2, 5, function(err, results){
    if(err){
        console.error(err);
        return;
    }
    else return results;
}));
*/

var result= java.callStaticMethodSync("MySumClass", "sum", 2, 5)
console.log(result)

Comments

0

alternative to the unmaintained java package npm install java-bridge

const { importClass, newInstance } = require('java-bridge');

const MyJavaClass = importClass('com.example.MyJavaClass');
const instance = newInstance('com.example.MyJavaClass');
instance.someMethod().then(console.log);

Comments

-1

By default, node-java will only have the objects from the default Java runtime library available. The classes you defined yourself (like MySumClass) will need to be compiled and added to your classpath before you can import and use them.

Make sure you compile your MySumClass.java file (using javac for example). For adding the compiled file to the classpath used by node-java, you can use java.classpath.

java.classpath.push('MySumClass.class');

2 Comments

Thank you for your help, I will try your solution when I get home
Hello! Sorry for getting so long to get back to you, I took a sick break. Even when I pushed the MySumClass.class as you suggested, the sum method can still not be found. I looked here and there about the classpath and tried alot of things, until finally I just had to make a jar file out of the MySumClass.class and push it to classpath to work. Still, thank you for leading me in the right direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.