JUnit exception in main? - java

I am trying to run a simple junit test using OS X and terminal. I have placed the Junit jar file inside my java folder. I was able to compile all of the files using:
javac -cp .:"/Library/java/junit.jar" *.java
It compiles just fine, with no errors. However when I try to run the command:
java TestRunner
It gives the error:
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.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
I cannot seem to find what I am doing incorrectly. Any help solving this would be greatly appreciated.

Like #ToddSewell said, the external libraries should be in your classpath for the execution too. Try this:
java -cp .:"/Library/java/junit.jar" TestRunner

Related

How to use Eclipse ClassPath via CMD or Bash?

I have one app which I want to dockerize. But first, I need to run it via console(cmd or bash). When I run it from Eclipse everything is okay, but when I run it from bash(java -jar project-18.5.0-SNAPSHOT.jar) I get this error
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at com.hp.spacecat.util.PropertyUtil.<clinit>(PropertyUtil.java:35)
at com.hp.spacecat.SpaceCat.main(SpaceCat.java:76)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
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)
... 2 more
I have that jar in m2 and he has this LogFactory class. Mvn clean install is success. But for some reason I can't run it via console. I don't know is there some way to get that classpath from Eclipse or there is another way to solve this.
Similar questions about this logging exception didn't help me.

Jar can not resolve org.apache.commons import

I am not particularly well experienced with Java at all and try to get a jar file running on my Ubuntu machine (https://sites.google.com/site/communitydetectionslpa/home).
However once I run the jar file with the command suggested by developers I receive the following error:
java -jar GANXiSw.jar -i test.ipairs
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/collections/map/MultiKeyMap
at Net.<init>(Net.java:38)
at SLPAw.<init>(SLPAw.java:146)
at SLPAw.main(SLPAw.java:2050)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.MultiKeyMap
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)
... 3 more
Apparently java is not able to import the org.apache.commons class properly. After some researching I checked if I have libcommons-collections3-java installed, which however is the case.
I read something about adding the library explicitly to my CLASSPATH, which however I also read to be not good practice.
What is the best approach to fix my issue?

How to use Jlex and Cup tools

i'm trying to learn something about JFlex and Cup tools on Ubuntu, because i have to use them for a school project. So i downloaded and installed JFlex and Cup. There are some examples in "jflex-1.6.1/examples", so i tried to run one of them. In particularly i tried to run the one in "jflex-1.6.1/examples/cup", following the instructions in the README file:
To compile:
jflex lcalc.flex
java java_cup.Main < ycalc.cup
javac Main.java
To run:
java Main test.txt
Files:
"Main.java" demo of a main program
"Makefile " makefile to compile and test the example
"README"
"lcalc.flex" the lexer spec
"output.good " how the output should look like for the test
"ycalc.cup" the parser spec
"test.txt " sample input for testing
With many difficulties, i have been finally able to compile, then to complete successfully the first three instructions. Now i have errors when i try to run: java Main test.txt. In particular i get this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: java_cup/runtime/Scanner
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: java_cup.runtime.Scanner
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
I don't have any idea on how to solve this. I tried also with other examples but the results were the same.
I hope that you can help me. Thank you!
I also got this error, and it's because I forgot to include java-cup.jar and java-cup-runtime.jar in my class path.
Try compiling like this:
javac -cp path/to/java-cup.jar Main.java
And then run your program like this:
java -cp path/to/java-cup-runtime.jar Main

JNI error has occurred when running java with OpenCV from terminal

I'm trying to compile a java-program written with OpenCV 3.2.0 from the terminal. It was originally written in Eclipse with OpenCV as a user library.
I compile it with this command:
javac -cp /opencv-3.2.0-java/build/bin/ *.java
It works with no error, the -cp command shows the path to the OpenCV jar. I then try to run everything with this command:
java -Djava.library.path="/opencv-3.2.0-java/build/lib" -cp jar/opencv-320.jar -classpath . Main
It gives me this error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Mat
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Mat
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 searched for solutions to this problem and came to the conclusion that it had something to do with native libraries. I use the -Djava parameter to link to the path of the library files from OpenCV, but it still doesn't work somehow. How do I fix this?
EDIT:
All the .java files are on a folder on the desktop. This is were the Main file is included. For example:
home/pi/Desktop/MultiSensor_v2.0_Java/Main.java
The path to the OpenCV jar:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/bin/opencv-320.jar
The path to OpenCV native libraries:
/home/pi/Desktop/MultiSensor_v2.0_Java/opencv-3.2.0-java/build/lib
-cp is short for -classpath. So I suspect that when you use both, the second one overwrites the first.
You can have multiple entries on the class path separated by : as stated in the documentation: Setting the class path. (actually, it looks like that is a typo in the doc, and it should be ; as a separator. You could try both)
If you execute your command from the directory home/pi/Desktop/MultiSensor_v2.0_Java/ that would make:
java -Djava.library.path="opencv-3.2.0-java/build/lib" -cp .:opencv-3.2.0-java/build/bin/opencv-320.jarr Main

java - error while running compiled program with external jar

I have a simple jsoup test app in a single folder, in which there are 2 classes - LyricsGetter.java and Main.java - and a .jar file with jsoup library. When I compile the files with command javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java everything compiles ok, but when I try to run with java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at LyricsGetter.getLyrics(LyricsGetter.java:16)
at Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
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)
... 2 more
On the other hand if I run with java -cp jsoup-1.8.2.jar Main, then I get Error: Could not find or load main class Main. So, what is the correct way to run this program?
You need to tell Java to look for classes in jsoup-1.8.2.jar, and the current folder (.).
On Windows, use:
java -cp jsoup-1.8.2.jar;. Main
or on Linux, OSX, or other Unix-like systems use:
java -cp jsoup-1.8.2.jar:. Main
(The difference is that the paths are separated by ; on Windows or : on *nix)

Categories