ClassNotFoundException: Running JUnit Tests in Eclipse - 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...

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>

running sbt class using only a jar

Is there any way to run sbt commands with only a jar instead of a project?
I've been having issues using scopt with java or scala commands, and it only seems to work with sbt.
Ideally something like
sbt --jar <jar name>/"run-main <options"
You'd probably want to package everything up into something you can execute. One possibility would be to create a fat jar using something like sbt-assembly.
Once you've built your jar, you can then:
java -jar /path/to.jar --your-options
Take note that at this point you can only do what would have been the equivalent of sbt run-main with the jar. You cannot of course invoke any of the other sbt commands on the jar created.

an error in spark sbt assembly

In spark installing README,I write command './bin/spark-shell',
I got some hint follwing:
Found multiple Spark assembly jars in /opt/spark-0.9.0-incubating-bin-hadoop1/assembly/target/scala-2.10:
spark-assembly-0.9.0-incubating-hadoop1.0.4.jar
spark-assembly_2.10-0.9.0-incubating-hadoop1.0.4.jar
Please remove all but one jar.
I ever try to remove one of them ,but I fail to success.
If somebody can deal with it ,please tell me.
thanks.
all you need to do is just cd to the assembly/target/scala-2.10 dir, and use the sbt to compile again.
this is caused by the incompatible between different compile method, you may used the maven and sbt both?
The problem is that the JARs are built into the assembly JARs when you run
sbt/sbt clean assembly
What I did is:
sbt/sbt clean package
This will only give you the small JARs. The next steps is to update the CLASSPATH in the
bin/compute-classpath.sh script manually, appending all the JARs.
With :
sbt/sbt assembly
We can't introduce our own Hadoop patch since it will always pull from Maven repo, unless we
hijack the repository path, or do a 'mvn install' locally. This is more of a hack I think.

Run junit4 test from cmd

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.

Is classpath, junit.jar and import org.junit statement in Eclipse independent?

Are classpath, junit.jar and import org.junit statement independent of each other in Eclipse?
After adding junit.jar on Windows 7 (environment) classpath I am not able to benefit from importing org.junit.*; statement in Eclipse for Java. Eclipse informs that the import org.junit cannot be resolved. Is it normal behaviour?
By using command line (cmd.exe) junit works fine:
java org.junit.runner.JUnitCore org.junit.tests.AllTests
However, within Eclipse I cannot use Junit classes.
The project build path is in charge in Eclipse. If junit.jar (of appropriate version) or the JUnit eclipse lib are on the build path, you can import junit classes. If not, not. The classpath environment variable is never a good idea.
Eclipse builds classpath to based upon what is called a 'build path' and invokes JVM with a -cp argument. JVM ignores CLASSPATH env variable if an explicit -cp jvm arg is passed to it so your Windows classpath setting is ignored.
Solution: set up your project build path correctly ie. add your jars there.