Running Junit test from command line - java

So I wrote a class called Item and using Junit wrote a test called ItemTest and I want to execute my test from a command line. This is steps that I followed:
set the path, to run java.exe and set the classpath to libraries
...> set path="C:\Program Files (x86)\Java\jdk1.7.0_40\bin"
...> set classpath=C:\Program Files (x86)\Java\hamcrest-core-1.3.jar; C:\Program Files (x86)\Java\junit-4.11.jar
then i simply run org.junit.runner.JUnitCore to see if added libs were in classpath
...>java org.junit.runner.JUnitCore
the output was
JUnit version 4.11
Time: 0,001
OK (0 tests)
Then I compiled without an error Item and ItemTest
C:\Users\User\IdeaProjects\My_project\src>javac .\main\java\Item.java .\test\java\ItemTest.java
Then try ro run this classes
C:\Users\User\IdeaProjects\My_project\src>java org.junit.runner.JUnitCore main.java.Item test.java.ItemTest
In the end I got this
JUnit version 4.11
Exception in thread "main" java.lang.NoClassDefFoundError: main/java/Item (wrong name: Item)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14 2)
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: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:190)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:86)
at org.junit.runner.JUnitCore.runMainAndExit(JUnitCore.java:47)
at org.junit.runner.JUnitCore.main(JUnitCore.java:40)
How to fix this ? Should I put Itemin classpath ?
UPD#1
Put this path in my classpath doesn't work (with and without src):
C:\Users\User\IdeaProjects\My_project\src>
UPD#2
When I want to run the class I adress to him as he stores in file system or in what packages he lays ? Because I don't have packages and so My_project apart from being project name is a module name.
SOLVED
This is the way - I add to classpath next paths:
The path to class Item:
C:\Users\User\IdeaProjects\My_project\src\main\java\;
The path to test class ItemTest:
C:\Users\User\IdeaProjects\My_project\src\test\java\;
Then simply run:
...>java org.junit.runner.JUnitCore ItemTest

You have to put the location of the .class files in the classpath. Even if you are in the same folder it won't find them if they are not in the classpath. That comes from DOS x .... anyone else.
In DOS the OS looks first in the current folder. Everywhere else the OS looks first in the path (or classpath for Java).
If Item is in the package com.example.Item and the class is in c:\users\test\myproj\target\com\example\Item.class then you must add c:\users\test\myproj\target\ to the classpath

Related

Java NoClassDefFound Error when trying to run

I'm fairly new to working with packages in java but I've got a file structure thats:
project
classes
src
com
main
Billing.class
Password.class
src
com
main
Billing.java
Password.java
Each java file has package package src.com.main; at the top.
I'm compiling from the project folder using javac -d ./classes/ ./src/com/main/*.java and it compiles fine. When I go to run it using java classes/src/com/main/Billing is gives me a NoClassDefFound exception. What am I doing wrong?
Edit: Entire Error:
java classes/src/com/main/BillingException in thread "main" java.lang.NoClassDefFoundError: classes/src/com/main/Billing (wrong name: src/com/main/Billing)
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)
Try
cd project/classes/
java -cp . src.com.main.Billing
Your class files are not in the classpath that may be the case.
If you are using maven and eclipse try the command mvn eclipse:eclipse
or
you can set the classpath using command prompt also where your .class files are located.
For example:
C:> set CLASSPATH=classpath1;classpath2..
i think you use other class in your class that is on other package or jar file if so! you need to define Class Path to enable java find your dependencies.

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)

Hadoop jar execution failing on class not found

I am running my hadoop job and it is failing on class not found. 4 java files in total.
logProcessor.java
logMapper.java
logReducer.java
logParser.java
Everything is in a com folder on unix and I have "package com;" in the first line in all classes
that means if you do below command
head -5 *java
You will see
package com;
in all 4 files.
logProcessor is the Driver class. All files are in "com" folder on unix.
ls -ltr com/
logProcessor.java
logMapper.java
logReducer.java
logParser.java
I compiled the java program and made a jar out of it.
hadoop jar /var/lib/hadoop-hdfs/xxxx/jarFiles/LogParser.jar com.LogProcessor /user/hdfs/flume/2015-03-30/03 /user/xxxx/output_xxx
It gives me below error:
Exception in thread "main" java.lang.ClassNotFoundException: com.RFCLogProcessor
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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:270)
at org.apache.hadoop.util.RunJar.main(RunJar.java:201)
First, you need to write your jar path in classpath. write directory path of your jar file into .bashrc file.
Then you can using below command:
hadoop jar directorypath/yourjarname.jar packagename.mainclassname outputpath
Everything that I was doing was right. Except that the classes which got created didnt have execute permission on them
So I did
chmod -R 777 com/
and jar'd it again and ran it with same above command and it executed.

Where to install library for Java on Ubuntu command line

I'm trying to get the Charva tutorial to work, but I don't think I'm installing it correctly. I added the .jar files to /jre/lib/ext, but I get this error when I try to run the tutorial file:
Exception in thread "main" java.lang.NoClassDefFoundError: Tutorial (wrong name: tutorial/java/Tutorial)
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)
Am I supposed to be running something else when I compile or am I installing the library the wrong way?
You can add the .jar files to the classpath using:
java -cp "Charva.jar" main.Class
If you need to add multiple jars, or you want it to be better structured in folders, put the jars under lib which is the common thing to do, and use:
java -cp "lib/*.jar" main.Class
If you're using and IDE, tag the question with the specific IDE to get specific instructions for your IDE.
I suggest you build and run your program in your IDE.
Once you have a working program you can build a JAR and when you specify it on the command line with
java -cp {path-to-my-jar}.jar mypackage.MyClass
you can put the JAR where ever you like.

Jsoup error at run time: Exception in thread "main" java.lang.NoClassDefFoundError

I am getting the following error
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at Main.main(Main.java:42)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
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
Now what I have done so far:
I created a libs folder in my project folder and added the Jsoup.jar there. Then in eclipse I click properties, add external Jar and added it. I see the referenced library.
But the issue still persist. I dont get compilation errors. So this is a runtime issue. Appreciate the help.
Edit:
I have found the following
If I compile using
javac -classpath /path/to/jsoup.jar myclass.java
and then
java -classpath /path/to/jsoup.jar myclass
It runs perfect.
Now. This is not working for me because I am going to use my application on a computer that does not have Jsoup. So how to I resolve this runtime issue?
Go to Run configurations... menu and check classpath for Run task. It looks like you add library for Compilation step, but it doesn't set at Run step.
Edit (after comment)
I dove into the issue and found that this dependency is needed for application container. So you just need to add this library to classpath of your app server. If you use Tomcat, just put the library to the %CATALINA_HOME%\lib directory.

Categories