0

I had a fully functional Java program that I am now trying to put into a package instead of having it in the default package. All of the .java files are in the mymap package, and I am trying to run black box tests in my MyMapTest.java file. The problem is that I don't know how to properly run this in the command line now that I have it in a package. I got stuck trying to google for answers.

I can get it to compile:

javac -cp "../junit.jar" *.java;

But when I try to run it as I previously had...

java -cp .:../junit.jar org.junit.runner.JUnitCore MyMapTest

I get the error java.lang.NoClassDefFoundError: mymap/MyMap for all of my tests. I guess the issue is that somehow I am not properly including mymap, perhaps in the classpath?

Similarly, I also have white box tests in MyMapWhiteBoxTest.java, where this file is also in the mymap package. I cannot figure out how to properly run these tests either. Trying to run it the same way, I get:

Exception in thread "main" java.lang.NoClassDefFoundError: MyMapWhiteBoxTest (wrong name: mymap/MyMapWhiteBoxTest)

For both cases, I have tried mymap/FILE or somehow also including the package in the classpath, but I'm definitely missing something.

jtebert
  • 1,303
  • 1
  • 19
  • 36
  • 1
    Possible Duplcate of http://stackoverflow.com/questions/4951695/receiving-wrong-name-noclassdeffounderror-when-executing-a-java-program-from-t/12044735#12044735 – Mark W Feb 24 '14 at 15:07

2 Answers2

0

If the main() is present in MyMapTest class then you can try the below command :

java -cp .:../junit.jar org.junit.runner.JUnitCore myMap.MyMapTest
Kakarot
  • 4,267
  • 2
  • 14
  • 18
0

After compilation: (javac -cp "../junit.jar" *.java;) what do you have in your current dir? You should have the file ./myMap/MyMapTest.class.

robermann
  • 1,682
  • 10
  • 19
  • I just seem to have MyMapTest.class – jtebert Feb 24 '14 at 15:22
  • How is that? Do you have your source file into ./myMap/MyMapTest.java? If you have ./myMap/MyMapTest.java, your compiled class must be ./myMap/MyMapTest.class – robermann Feb 24 '14 at 15:32
  • The mymap folder should contain the files in the mymap package, correct? And MyMapTest.java is just importing mymap, so it shouldn't be in the mymap subfolder? – jtebert Feb 24 '14 at 15:39
  • So this should work: java -cp .:../junit.jar org.junit.runner.JUnitCore MyMapTest But if MyMapTest is declared in a package, you should run: java -cp .:../junit.jar org.junit.runner.JUnitCore MyMapTest – robermann Feb 24 '14 at 15:41
  • OK, I got MyMapTest to work. I had something weird with the subfolders. But I still can't get MyMapWhiteBoxTest to work. I try `java -cp .:../junit.jar org.junit.runner.JUnitCore mymap/MyMapWhiteBoxTest` and JUnit says `Could not find class: mymap/MyMapWhiteBoxTest` – jtebert Feb 24 '14 at 15:43
  • in the current dir do you have **mymap/MyMapWhiteBoxTest.class** ? – robermann Feb 24 '14 at 15:47
  • No, actually. Why would that not be compiling? – jtebert Feb 25 '14 at 14:40
  • javac does not compile recursivelly. See for example http://stackoverflow.com/questions/6623161/javac-option-to-compile-recursively As pointed, you can do: `:: Windows` `> dir /s /B *.java > sources.txt` `> javac @sources.txt` – robermann Feb 25 '14 at 19:42
  • Thanks for that link. I now have a mymap/MyMapWhiteBoxTest.class, but JUnit still says `Could not find class: mymap/MyMapWhiteBoxTest` – jtebert Feb 26 '14 at 00:28
  • You have to run `java -cp .:../junit.jar org.junit.runner.JUnitCore mymap.MyMapWhiteBoxTest` and not `java -cp .:../junit.jar org.junit.runner.JUnitCore mymap/MyMapWhiteBoxTest` (see the '.' in the class name) EDIT: start reading from here: http://stackoverflow.com/questions/9510932/java-package-vs-folder-structure-what-is-the-difference – robermann Feb 26 '14 at 09:03
  • Even better, start here: http://docs.oracle.com/javase/tutorial/java/package/managingfiles.html – robermann Feb 26 '14 at 09:13