Community
cancel
Showing results for 
Search instead for 
Did you mean: 
GGrad
New Contributor I
1,100 Views

Edison - Use Google-Gson library with Eclipse (Java)

I'm trying to use the Google-Gson library with my Java project in Eclipse. In order to use the Mraa library I had to add the mraa.jar file, as well as use the following command in my code:

System.loadLibrary("mraajava");

I'm assuming this line is looking for the shared object file libmraajava.so (correct me if I'm wrong), but I can't find anything like that for Google-Gson (files located at http://repo1.maven.org/maven2/com/google/code/gson/gson/2.5/ http://repo1.maven.org/maven2/com/google/code/gson/gson/2.5/).

I can use the Gson library in just a normal Java project by simply adding the Jar, but with the Edison I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson

at GsonTest.readWriteGson(GsonTest.java:1204)

at GsonTest.main(GsonTest.java:233)

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

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

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

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

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

I've referenced the Jar as such:

Any help would be greatly appreciated! Thanks!

5 Replies
Sergio_A_Intel
Employee
111 Views

Hi,

What Eclipse IDE have you downloaded? The Eclipse IDE from the Downloads section works for applications written in C and C++. The latest version is Luna SR2 (4.4.2). Are you trying to use Java code with this IDE?

Sergio

GGrad
New Contributor I
111 Views

Yeah, I've got 4.4.2, downloaded from Intel. I can run Java projects just fine. I'm just having trouble using third-party libraries.

GGrad
New Contributor I
111 Views

And actually, it turns out I can get rid of the following line, and the Mraa library loads just fine.

  1. System.loadLibrary("mraajava");

Apparently importing the library into the build path is enough, so I have no idea what the System.loadLibrary call is doing. So I took that out.

UPDATE: I ended up with a workaround that isn't pretty, but I'll take it for now. Google has the source files available for their library, so I just added all the Java files from the Gson library directly into my project. I mean, that's basically what should be happening when I added the Gson Jar, so I'd still like to figure out why adding a third-party library via the .Jar won't work. I've tried with a couple different libraries and have yet to find success. None of the solutions I've found for a NoClassDefFound error have worked, and I'm sure I'm going to need another library in the future.

Any ideas why the libraries found in the IoT Sensor Support window will work when others won't?

Sergio_A_Intel
Employee
111 Views

Not sure why you are receiving that error, what command have you used to compile the Java files? The path seems to be correct; according to https://software.intel.com/en-us/node/596288 IoT - Running Java* IoT Applications outside of Eclipse* | Intel® Developer Zone the .jar files and Java shared libraries needed on runtime for using MRAA & UPM is /usr/lib/java.

Do you have the latest MRAA and UPM libraries installed?

Sergio

GGrad
New Contributor I
111 Views

That link is exactly what I needed! For anyone else using Java and Eclipse, Apparently adding the external JARs in Eclipse isn't enough, one also must copy the JARs over to the /usr/lib/java folder on the Edison (it's really easy using WinSCP). Once I did that, I no longer got my ClassNotFoundException during runtime.

Thanks so much for the help!

Reply