ClassNotFoundException: How to properly load a jar library?

1,265 views
Skip to first unread message

Luca Stanziano

unread,
May 11, 2015, 10:36:30 AM5/11/15
to node...@googlegroups.com
I currently have:

---- node.js File ----
.....

var java = require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
java.classpath.push("./libs/mylib-0.1.jar");
java.import("api.model.MyObject");

var myObject = java.newInstanceSync("api.model.MyObject");

myObject.newInstance("name");

.......


my-lib.01.jar file with the following structure inside:

--api
  |--model
     |--MyObject.class
--lib
  |--gson-2.3.1.jar



and MyObject.class contains:

package api.model;

import com.google.gson.JsonObject;

public class MyObject {


        private String name;

protected MyObject() {

}

public static MyObject newInstance(String name) {

                MyObject instance = new MyObject();
                instance.name = name;
return instance;
}


        public JsonObject toJson() {
JsonObject jobj = new JsonObject();
jobj.addProperty("name", name);
        
                return jobj;
}
}


When I run it the following exception is thrown:



[18648:0511/161147:ERROR:nw_shell.cc(335)] Error: Could not create class api.model.MyObject
java.lang.NoClassDefFoundError: com/google/gson/JsonElement

Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonElement

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

at java.lang.ClassLoader.loadClass(ClassLoader.java:358)


    at Error (native)
    at Java.java.import (C:\Workspace\WebStorm\GMDashboard\node_modules\java\lib\nodeJavaBridge.js:205:20)
    at Object.<anonymous> (C:\Workspace\WebStorm\GMDashboard\server.js:16:12)
    at Module._compile (module.js:451:26)
    at Object.Module._extensions..js (module.js:469:10)
    at Module.load (module.js:346:32)
    at Function.Module._load (module.js:301:12)
    at Module.require (module.js:356:17)
    at require (module.js:375:17)
    at window.require (<anonymous>:4:17)
[18648:0511/161147:INFO:CONSOLE(205)] "Uncaught Error: Could not create class api.model.MyObject
java.lang.NoClassDefFoundError: com/google/gson/JsonElement

Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonElement

at java.net.URLClassLoader$1.run(URLClassLoader.java:366)

at java.net.URLClassLoader$1.run(URLClassLoader.java:355)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(URLClassLoader.java:354)

at java.lang.ClassLoader.loadClass(ClassLoader.java:425)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)

at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

", source: C:\Workspace\WebStorm\GMDashboard\node_modules\java\lib\nodeJavaBridge.js (205)
[Finished in 152.7s]


What am I doing wrong?

Joe Ferner

unread,
May 11, 2015, 10:59:44 AM5/11/15
to node...@googlegroups.com, luca.st...@gmail.com
I don't see gson being pushed to the classpath at the top.

Luca Stanziano

unread,
May 11, 2015, 11:36:07 AM5/11/15
to node...@googlegroups.com
Ok, so I have to export all the dependencies separately from the jar and then import all of them in Node.JS. I hoped that adding the JAR to the classpath all the libraries inside the JAR were added automatically too.

To make the code run I exported the dependencies in a separate folder (myObject_lib) and added them to the classpath.


var java = require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
java.classpath.push("./libs/myObject-0.1.jar");
java.classpath.push("./libs/myObject-0.1_lib/gson-2.3.1.jar");

var myObj = java.import("api.model.MyObject");

myObj.newInstance("name");

Joe Ferner

unread,
May 11, 2015, 11:39:27 AM5/11/15
to node...@googlegroups.com, luca.st...@gmail.com
You can try using this project that I created: https://github.com/joeferner/node-java-maven

It has a lot less testing then node-java but it might help with loading all your dependencies.

But, yes you need to include all jar files that java would need to run your code. So if you can run "java -cp ... <your-class-name>" then you can use it in node using the same classpath.

mrinalini upadhya

unread,
Jun 14, 2019, 1:20:14 PM6/14/19
to node-java
Hi Joe,

I tried used "node-java-maven". But the issue I'm facing is that the java classpath gets appended with all the maven dependencies only with the mvn function like so:

mvn(function(err, mvnresults) {

Java class path valid only here.

});

if i try to add any new imports outside the mvn() itself the java classpath would not be valid since I see that mvn() is that last function that gets executed. Could you please share the right way to use node-java-maven?
Reply all
Reply to author
Forward
0 new messages