Junit and Java path issues - OS X
I am trying to run the sample tests that come with junit4.7 and with some difficulty.
java
observes my CLASSPATH
:
me@dinosaurhunter ~/Desktop> export CLASSPATH=
me@dinosaurhunter ~/Desktop> echo $CLASSPATH
me@dinosaurhunter ~/Desktop> java junit.textui.TestRunner junit.samples.AllTests
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/TestRunner
me@dinosaurhunter ~/Desktop> source /etc/profile
me@dinosaurhunter ~/Desktop> echo $CLASSPATH
:/Library/Java/Extensions/junit/:/Library/Java/Extensions/junit/junit.jar
me@dinosaurhunter ~/Desktop> java junit.textui.TestRunner junit.samples.AllTests
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/Test
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at junit.runner.BaseTestRunner.loadSuiteClass(BaseTestRunner.java:207)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:100)
at junit.textui.TestRunner.start(TestRunner.java:179)
at junit.textui.TestRunner.main(TestRunner.java:138)
but as you can see it cannot find junit/framework/Test
... I looked in /Library/Java/Extensions/junit/junit.jar
and it is turned on, however.
/Library/Java/Extensions/junit/junit.jar
is a symbolic link. This is normal?
source to share
Try putting the junit JAR file directly into the Extensions directory instead of creating a subdirectory for it. I just copied junit-4.6.jar to / Library / Java / Extenstions directory and executed TestRunner class with no problem
% java junit.textui.TestRunner Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class
Removing a library from the Extensions directory results in an expected exception
Exception in thread "main" java.lang.NoClassDefFoundError: junit / textui / TestRunner
source to share
Ok, I just downloaded JUnit 4.7, unpacked the zip file, changed the directory in the folder and ran the following command successfully:
$ java -cp .:junit-4.7.jar junit.textui.TestRunner junit.samples.AllTests
.........................................
.........................................
.........................................
.......
Time: 3.255
OK (130 tests)
It was on OSX.
I think your classpath is a little messed up in your example. Try the following:
.:/Library/Java/Extensions/junit:/Library/Java/Extensions/junit/junit.jar
See the differences? I added .
(current directory) and I removed the trailing slash from the junit directory.
UPDATE: I just tested with a symlink and it seems to work too:
$ ln -s junit-4.7.jar junit.jar
$ java -cp .:junit.jar junit.textui.TestRunner junit.samples.AllTests
.........................................
.........................................
.........................................
.......
Time: 2.569
OK (130 tests)
source to share
Some things to explore:
Is the junit.jar file the location you specified and is it readable? Is CLASSPATH exported by / etc / profile? Does it work when you install "-cp $ CLASSPATH" instead of the command line? Try removing the leading colon in the classpath - it shouldn't be there.
source to share
Perhaps using a symbolic link is causing your problem. Try adding the jar pointed to by the symlink and try again.
However, looking at the java man page for mac os x there seems to be no such limitation.
source to share