Run junit4 test from cmd - junit4

I tried to run junit4 test case from command line using:
java -cp junit-4.8.1.jar;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
but I got the following error:
java.lang.NoClassDefFoundError: graph/shortestgraphpath;
while the test case is working without any problems in eclipse.
Hint: in eclipse, shortestgraphpath was added in Referenced Libraries.

You need to the jar file containing shortestgraphpath to java class path.
java -cp junit-4.8.1.jar;test\Dijkstra; test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
The class path is the value that you pass to java with -cp so in your question you just supply junitand your compiled classes.
Try updating it with the jar file with the missing class.
java -cp junit-4.8.1.jar;<path to jar file>;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
You might have to add additional jar files as well. I recommend that you take a look at some build tool to help you build and run your java applications for example Maven, Gradle, Buildr.

Related

how to run scala .jar with external jar files in terminal

I have my .jar built from scala classes and it has an external dependency with other.jar. Please suggest how should I run my jar files in terminal. The command I tried is
$scala my_scala.jar external.jar
It works same way as running java program. Try this
scala -classpath <your_scala_jar>:<external_jar> <package.MainClass>

No such file or class on classpath: com.<name>.<abc>.<classname> when executing scala jar using command line

I am getting the
No such file or class on classpath: com
when executing the scala uber jar using the below command.
scala -classpath kafka-scala-1.0-SNAPSHOT.jar com.< name >.< abc >.KafkaAggregateConsumerApp
I am using scala 2.11.12
From the error it looks like you might have a space after the com in your command. Re-check that please.
Also, if you already have an uber jar you can run the jar directly using the java command.
java -cp kafka-scala-1.0-SNAPSHOT.jar com.foo.bar.KafkaAggregateConsumerApp

Maven 1.0 snapshot .jar cannot find class name

I wrote maven project and it run correctly in Eclipse but I want to run maven project in cmd line so I write this line in cmd
java -cp target/my-app-1.0-SNAPSHOT.jar com.my.App
I have this error
Could not find or load main class com.my.App
How I solved it?

ClassNotFoundException: Running JUnit Tests in Eclipse

I have a project with src, classes & tests. I keep my JUnit tests in the tests folder.
I keep getting a ClassNotFoundException while running them in Eclipse.
I tried running them using from the Command line.
javac -d classes src\brick\*.java test\brick\*.java
The compiler reports 20 errors telling me that the package org.junit does not exist.
Could anybody help me?
Thanks.
What are you using to manage your dependencies (maven, contained in a lib dir)? Are all of the necessary jars in your classpath?
Javac needs to know where your JUnit jar is located in order to compile your classes.
You should try something like:
javac -cp <path to junit jar> -d classes src\brick\*.java test\brick\*.java
When in Eclipse, you need to add the JUnit jar to your project. Normally this is
done automatically if you have a test case...

How o run a NetBeans-built Scala application jar from command line outside IDE?

A program of mine (written in Scala 2.8) works fine when launched by means of NetBeans IDE. But when I try to run it from outside, with "java- jar", it says "Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject...". Putting all the libraries, incl. Scala runtime inside the same dir as the jar to be run doesn't help. If i try to run the jar with scala itself, it complains that it can't decode it as utf-8 (it expects a scala source rather than a jar, I suppose). So how do I run a Scala application at all?
UPDATE: For those who come here later having the same question I'd recommend to read comments under barjak's answer (including those latest ones hidden), the answer is there. VonC also gives some interesting links on the subject.
The -jar and -classpath of the java command are mutually exclusive : you can't do java -jar YourScalaProg.jar -classpath scala-library.jar
If you want to run your application with java -jar, then the full classpath must be specified in the Class-Path section of the jar's manifest.
You can run your application using only -classpath, like that : java -classpath YourScalaProg.jar:scala-library.jar your.package.MainClass.
Are you using scala-library.jar as described in Adventures with Scala blog post?
java -classpath scala-library.jar:. YourClass
or:
java -classpath scala-library.jar:yourApp.jar YourClass
Where YourClass is was your scalac compiled Scala code.
You will find the same scala-library.jar used in the SO question "Creating a jar file from a Scala file" (or in the blog post "the not so elegant way of creating an executable jar from scala code").