The syntax for running Junit from commandline in Mac - java

I want to run Junit from commandline in Mac OS but Im not able to get it to work as expected with a simple test class that I have created.
I have downloaded junit-4.12.jar for this purpose and been writing a very simple test class with one simple test method.
Im running with the following command to compile the class:
javac -cp .:/Full/path/to/junit/junit-4.12.jar DemoTest.java
..and running the following command to execute the compiled class:
java -cp .:/Full/path/to/junit/junit-4.12.jar org.junit.runner.JUnitCore DemoTest
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class DemoTest {
#Test
public void evaluate(){
assertEquals(true, 1<2);
}
}
I'm expecting to see "test pass" when I am running the junit class from commandline, because that's what it should generate. Instead Im getting the following error message (please note that Im only pasting the first line of error for the sake of simplicity):
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
Does this mean that Im only missing the hamcrest.jar file to be able to run the application as expected?
Please let me know if you need the complete error stacktrace!

As the error message
Exception in thread "main" java.lang.NoClassDefFoundError:
org/hamcrest/SelfDescribing
mentioned you miss the hamcrest-core library in your class path. Download it and add it to the classpath
java -cp .:/Full/path/to/junit/junit-4.12.jar:/Full/path/to/hamcrest/hamcrest-core-1.3.jar org.junit.runner.JUnitCore DemoTest

Related

How to include a third-party jar-file in compilation and runtime using command prompt in Windows 10?

I'm trying to understand the inclusion of third party jar files in a java project using only the command line in Windows 10.
Specifically, I try to include the file json-20200518.jar in my "project" so that I can use the java object JSONObject in the project.
My java file:
package com.mypackage.example;
import org.json.JSONObject;
class Example {
public static void main(String[] args) {
// ... program logic
}
}
location of my java file (Examp.java):
./com/mypackage/example
location of jar file:
./jars
using cmd win10 I compile:
javac -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Examp.java"
compilation is successful.
Run:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" com.mypackage.example.Examp
I get a report:
Error: Could not find or load main class com.mypackage.example.Pokus
Caused by: java.lang.ClassNotFoundException: com.mypackage.example.Pokus
Second attempt:
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar" "C:\Users\pfort\Desktop\java\com\mypackage\example\Pokus"
But the same error message comes back to me.
Where am I going wrong? Is it the wrong structure? I don't get it, the compilation is successful but the run does not work.
The compiled Examp.class file isn't part of json-20200518.jar, so you'll need to add the directory containing it to the command line. Assuming it's the current directory (.):
java -cp "C:\Users\pfort\Desktop\java\jars\json-20200518.jar;." com.mypackage.example.Examp

How do I pass an additional jar do my java command when running application?

I have a java gradle projectA that references another java gradle projectB, that builds as a lib.
My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. But when I try to run the application from the command line I get an error...
Exception in thread "main" java.lang.NoClassDefFoundError:
dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by:
java.lang.ClassNotFoundException: dawcore.SamplerInstrument at
java.net.URLClassLoader.findClass(URLClassLoader.java:382) 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) ... 1 more
It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. But I don't know how to have it successfully load those classes.
my current run command that does not reference the jar, is as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI
Running this gives the initially mentioned error.
I then read the docs on java -cp argument. It says to provide additional classpath directories, to separate them by semicolon.
I susequently updated my run command to be as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI
This gives me the following error....
Error: Could not find or load main class DawCLI
My main function is as follows..
import dawcore.*;
public class DawCLI {
public static void main (String [] args) throws IOException {
SamplerInstrument samplerkick = new SamplerInstrument();
}
}
According to the docs I seem to be doing this correctly, but am still getting errors. Any insight on this would be greatly appreciated!
Thanks
The solution was to replace the delimiting semi-colon with a colon instead. Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon.
This could be because I am running on linux and those docs indicate Windows.
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

Exception Error in creating an instance of RandomDataImpl

I am getting this exception error at
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/math3/random/RandomDataImpl
at this line of code:
randomData_ = new RandomDataImpl();
and this is how the class is imported in the code :
import org.apache.commons.math3.random.RandomDataImpl;
i have added all of the math common classes to the classpath while running the code. any ideas whats going on ?
It looks like you did not set the appropriate classpath.
You can set it as follows:
java -classpath lib/*.jar:. mypackage.MyClass
Try that and I am sure it will work.
If you are using maven, then read this article
You can also check your current classpath with:
System.getProperty("java.class.path")
Just write simple app printing this property out and run it just as you run an app with which you are having problems.

NoClassDefFoundError in java command line

I am using command line to run java file. Here's my files:
Class files: workspace/test/src/test/test.class
Java files: workspace/test/src/test/test.java
My command line is (CLASSPATH is already setted):
D:\Twitter\workspace\test\src\test>java test
And my error is:
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: tes
t/test)
Anyone know how to solve this?
You have to provide the fully qualified class name (FQCN) to the java command. Your class is named test (Test as a class name would be better) and is in the package test. Therefore the FQCN is test.test.
To start your program you have to call java test.test. from D:\Twitter\workspace\test\src.
Try:
D:\Twitter\workspace\test\src\test>cd ..
D:\Twitter\workspace\test\src>java test.test

NoClassDefFoundError on simple jar file

public class RunScript {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I created a Java project and a package called com.klong
Inside of the package I have one .java file with the above code in it.
I export it into a runnable jar file. Then I try to run the jar in a command line using this:
java test.jar
When I try that, I get the following error
C:\Users\IBM_ADMIN\Tracing stuff>java test.jar
Exception in thread "main" java.lang.NoClassDefFoundError: test.jar
Caused by: java.lang.ClassNotFoundException: test.jar
at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
at java.lang.ClassLoader.loadClass(ClassLoader.java:653)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:619)
Could not find the main class: test.jar. Program will exit.
I've tinkered with exporting into a normal jar file and such. I've looked at other questions about this error. I've tried using commands such as set classpath=BLEH
This project is as simple as can be so hopefully you can help me figure out this pesky error!
You need to run java -jar test.jar, what you're currently doing is asking java to look for compiled classes with the name test.jar.

Categories