Adding json jar to classpath java - java

I'm very new to Java world. I want to add the Json lib to compile my program. I downloaded the file from here.
When I tried to compile the program with
javac -classpath json.jar MyClassName.java
I'm getting the error :
Note: JsonSimpleExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
And when I tried to run :
java MyClassName
I'm getting:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at MyClassName.main(MyClassName.java:9)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
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:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
Where I'm making the mistake?

You have to use the -classpath when running with java as well.
java -classpath json.jar MyClassName

You're making two mistakes:
The message you get from the compiler is a note, and not a compilation error. Follow the instructions to get details
You forget to set the classpath when running the app:
java -classpath json.jar;. MyClassName
(assuming you're on windows, and you're in the directory containing MyClassName.class). If you're on Unix, replace the ; by a :.
Note that it's bad practice to put classes in the default, root package.

Reason to the error was ,the class was present during compile time and let's application to compile successfully and linked successfully but not available during run-time.
java -classpath json.jar MyClassName (or)
java -cp json.jar MyClassName

The first output is warning from the Java compiler about use of unchecked code (like raw collections). The class file would have been successfully generated.
If you want to fix these warnings, then you can compile with -Xlint command line switch and fix/suppress the unchecked/unsafe warnings.

Related

Encountered error while trying JCov java coverage utility

Almost everywhere in INTERNET there are these basic steps:
• Compile java files as usual
javac <source-files>
• “Instrument” the byte code
java -jar jcov.jar Instr <application classes>
• Run the code
java -classpath ...:jcov_file_saver.jar ...
• Create a report
java -jar jcov.jar RepGen <jcov xml file> demo
I was able to instrument class files and jar files both, but couldn't run jar one.
Faced this error:
$ java -cp .:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tdk/jcov/runtime/Collect
at BubbleSort.main(BubbleSort.java:49)
Caused by: java.lang.ClassNotFoundException: com.sun.tdk.jcov.runtime.Collect
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)
... 1 more
)
Can anyone please help me out or direct me to some web-page where I can understand this?
You also need to provide the jcov.jar file as well because it contains the com/sun/tdk/jcov/runtime/Collect class.
To supply the jcov.jar at run time, run this
java -cp .:$JCOV/jcov_file_saver.jar -Xbootclasspath/a:$JCOV/jcov.jar -jar BubbleSort.jar
Using this resolved the issue faced thanks to sumedh's answer which pushed me to understand about various kind of classpaths.
Executing instrumented jar:
java -cp . -Xbootclasspath/a:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar

add library to java, CLASSPATH, jar, linux

I've been browsering the Internet for a couple of hours and I am unable to find an answer to my question. The library I'm trying to add is JGraphT
I'm new to Java and I wanted to add a free graph library. I downloaded all .jar files and then the issues startet. What step by step should I do?
I found information about compiling with -cp or -classpath or addng .jar to CLASSPATH (I'm using Linux and I write my programms in gedit (obligatory for my studies) and compile it with terminal). But I wonder what should I do step by step?
What do I have so far:
I have downloaded multiple .jar and they all sit in one folder with
the xxx.java file I would like to compile
do I need to change CLASSPATH? How to do it? How should I compile and
run my program after changing CLASSPATH? The ordinary way(javac
xxx.java; java xxx) or should I change sth?
or maybe I don't need to change CLASSPATH just add -classpath while
compiling? If so, what should the compile anr run commend look
like?
Also I have already tried using -cp... I'm enclosing my lines in terminal. It compiled correctly, but when I tried to run it, I received strange errors. I'm sure the code is correct since it was given in the library as a way to test wether or not is it installed correctly.
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ javac -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT.java
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$ java -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT
Exception in thread "main" java.lang.NoClassDefFoundError: HelloJGraphT (wrong name: org/jgrapht/demo/HelloJGraphT)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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 sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
wiktoria#wiktoria-1015PW:~/programowanie/grafy/java/testy$
Let's say your class belongs into the package net.example.graph. This means the real name of the class is net.example.graph.HelloJGraphT.
This also means you have a directory structure like this:
project_dir/net/example/graph
Go to the project_dir folder, then try this:
javac -cp <path to jgrapht JAR> net/example/graph/HelloJGraphT.java
java -cp <path to jgrapht JAR> net.example.graph.HelloJGraphT

Java SQLite org.sqlite.JDBC classpath broken?

I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC
my code compiles and runs fine on Windows.
But when I tried moving it to Ubuntu it started showing this:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
at mall.AllegroReader.<init>(AllegroReader.java:33)
at mall.Mall.main(Mall.java:31)
I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar
with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk.
I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.
I have no idea what is going on. Pls help.
Here is what happens inside my dist directory:
work1#workwork:/var/www/mall/dist$ ls
mall.db Mall.jar Mall.jar.old sqlite-jdbc-3.8.4.3-SNAPSHOT.jar
work1#workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall
The classpath is ignored when you use -jar.
You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.
Error: Could not find or load main class Mall.main. all files are there,
my main class comes from Mall.java and is in mall package which
compiles to Mall.jar
So the correct command line is:
java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
OP findings
to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall
it showed
mall/Mall.class
so I should have used mall.Mall as the class to run (instead of pulling my hair)
After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large!) I decided to write the offending piece of code in PHP.
It proved to be more portable than Java in my case.

JUnit sample program compiling but giving runtime error

I am starting off with JUnit in Ubuntu and tried to execute the sample program given here. I have also followed instructions as given in the same link. The code compiles succesfully but gives runtime error as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
at TestRunner.main(TestRunner.java:7)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
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:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
So far this is what I have got. This error occurs if the $CLASSPATH variable isn't set to the location of the classes required by it.
> echo $CLASSPATH
/home/webyog/workspace/JUNIT/junit-4.11.jar:.
My java files and class files are in /home/webyog/JUNIT_WORKSPACE/ and all the libraries and the JUnit jar file is in /home/webyog/workspace/JUNIT/.
I executed these in order:
javac TestJunit.java TestRunner.java -classpath /home/webyog/workspace/JUNIT/junit-4.11
(This generated 2 files TestRunner.class and TestJunit.class)
java TestRunner -classpath $CLASSPATH
(This gives the error)
What I am I missing? Please help.
In your question, you have /home/webyog/workspace/JUNIT/junit-4.11.jar:. for $CLASSPATH but state:
the JUnit jar file is in /home/webyog/JUNIT/
(note the workspace missing)
That could explain the error you are seeing.
Try with:
cd /home/webyog/JUNIT_WORKSPACE/
java -cp '/home/webyog/JUNIT/junit-4.11.jar:.' TestRunner
Also you might want to check out some build tool to make you life easier (maven, ant, gradle, ...)

Compile error with JOGL

I try to compile a simple Java program with JOGL OpenGL, from the command line. In the Eclipse all works fine, when I compile it from the command line with javac I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
...
Could not find the main class: SimpleScene. Program will exit.
I already add to -classpath all the .jar files I find in JOGL directory. The Makefile to build the program is the following:
SimpleScene:
javac -Xlint:deprecation -classpath \
$(jogl)/joal.jar:\
$(jogl)/jogl.os.x11.jar:\
...
$(jogl)/nativewindow.awt.jar:\
$(jogl)/jogl_cg-natives-linux-i586.jar:. \
SimpleScene.java
I had a similar problem and figured out my simple mistake. GLEventListenere is an interface; not a class.
http://download.java.net/media/jogl/builds/archive/jsr-231-beta5/javadoc_public/javax/media/opengl/GLEventListener.html
I fixed my problem after realizing this because my code that uses GLEventListener should use the keyword 'implements' instead of 'extends'.

Categories

Resources