ashgromnies
ashgromnies

Reputation: 3335

Junit and Java classpath woes - OS X

I'm trying to run the sample tests that come with junit4.7 and having some difficulty.

java is respecting 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's unable to find junit/framework/Test... I looked in the /Library/Java/Extensions/junit/junit.jar and it is included, though.

/Library/Java/Extensions/junit/junit.jar is a symlink. Is that okay?

Upvotes: 2

Views: 6644

Answers (5)

John Haager
John Haager

Reputation: 2115

Try putting the junit JAR file directly into the Extensions directory instead of creating a subdirectory for it. I just copied the junit-4.6.jar into the /Library/Java/Extenstions directory and executed the TestRunner class with no issues

% java junit.textui.TestRunner
Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class

Deleting the library from the Extensions directory results in the expected exception

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/TestRunner

Upvotes: 5

ZZ Coder
ZZ Coder

Reputation: 75456

Did you build the JAR yourself? This looks like the JAR is built without annotation.

If you compile the JUnit with

  javac -proc none ...

You will get this error.

Upvotes: 3

Asaph
Asaph

Reputation: 162801

Ok, I just downloaded JUnit 4.7, unpacked the zip file, changed directory into 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)

That was on OSX.

I think in your example, your classpath is a little messed up. Try this:

.:/Library/Java/Extensions/junit:/Library/Java/Extensions/junit/junit.jar

See the differences? I added the . (current directory) and I removed the trailing slash from the junit directory.

UPDATE: I just tested with a symlink and that appears 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)

Upvotes: 5

Sean Owen
Sean Owen

Reputation: 66886

Some things to investigate:

Is the junit.jar file in the location you specify, and is it readable? Is CLASSPATH exported by /etc/profile? Does it work when you set "-cp $CLASSPATH" instead on the command line? Try removing the leading colon in the classpath -- should not be there.

Upvotes: 0

hhafez
hhafez

Reputation: 39750

It is possible that using a symlink is causing your problem. Trying adding the jar that symlink is pointing to and try again.

However looking at the java man page for mac os x there doesn't seem to be such a restriction.

Upvotes: -1

Related Questions