Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test class cannot find other class

 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to write the simple program from page 106 in Head First Java (Second Ed.). The idea is that this program would create a test for a very simple Battleship type game involving nothing more than a simple guess, matching the guess with a location in an array, and then reporting back accordingly (on whether you guessed the correct number in the array). The problem is my test class keeps reporting it cannot find the SimpleDotCom class. I'm sure it's something simple, but I'm just not seeing it. Thank you very much for the help ^^

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is you directory structure?
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RIght now all I have is a directory named SimpleDotCom with both of the above .java files in it.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the Java file names? And how do you run the program?
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file names are the same as the class (ie: the SimpleDotCom class is in SimpleDotCom.java). I honestly don't know, but isn't that always the case? Also, I've yet to be able to run the program due to the compiler error I receive with the above problem.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keitaro,

I don't see any compiler error in your program and I also tried running your program and it works fine too.
The only thing I can think is if you have same name for your file as your class name.
So accordingly, your file names should be "SimpleDotCom.java" and "SimpleDotComTestDrive.java".

How are you compiling the classes? Are you using any IDE(Eclipse/Netbeans) or do you use the command line(what command do you use?).

Also, though I don't think this might be a problem, but the for-each construct that you are using does not work on 1.4 or earlier versions of Java.



-Ninad
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh and Welcome to Java Ranch!
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was also able to use that exact same code, no modifications, and it compiled without error both from a command line and in eclipse (not that it should really make a difference). This would lead me to believe it could be a file naming typo. Double check file names are correct. Easy thing to overlook when first starting if you aren't using an IDE. Let us know how it goes.

Welcome to Java Ranch!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell us more details, including the exact command you are using to compile the class and the exact error message. Also copy the structure of the directory and the CLASSPATH environment variable, as described here.
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much! I am using Java 1.6_u21 and the only thing I'm using to write these files is Notepad++ on WinXp 32b. So once I wrote these with Npp, I saved them as SimpleDotCom.java and SimpleDotComTestDrive.java. I'm using the compiler that is issued with the JDK, so the command I'm running is 'C:\Program Files\Java\jdk1.6.0_21>.\bin\javac "C:\Documents and Settings\user\My Documents\Projects\SimpleDotCom\src\SimpleDotComTestDrive.java"' and the exact error I'm getting is

 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's partially a CLASSPATH problem. Edit your system CLASSPATH environment variable by adding .; (dot-semicolon) to its beginning. Also why are you in the folder called S rather than in src? Navigate to src which you might get to from S by writing
cd ..

Then try again.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Campbell is accurate. I was able to recreate your error after modifying my own CLASSPATH away from what it should be. Once I reset it I was able to get rid of the error. (I realize Campbell's geniusness (thats right...totally a word) needs no backup but figured I'd agree since I ended up at same conclusion)

If you are still having issues just add C:\Documents and Settings\user\My Documents\Projects\SimpleDotCom\src to your CLASSPATH directly, you should absolutely be able to compile it with that in there. I suppose this is one reason why I do love me a good IDE.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: Also why are you in the folder called S rather than in src? Navigate to src which you might get to from S by writing
cd ..

Then try again.


It is not a folder S, the message has continued on the next line.
Its definitely a classpath problem, I reproduced it. Use Campbell's suggestion. You could also traverse to the directory where you are storing your java files and simply say...
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went ahead and changed the CLASSPATH as suggested and then tried the command I was doing previously (from the /java/bin folder and directly specifying the .java file) and got the same error. I then cd'd into the /simpledotcom/src folder and did "javac *.java" which compiled both of my class files, but then when the program runs ("java simpledotcomtestdrive.class") it gives me this error:


Went from one thing right to another lol.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just type "java SimpleDotComTestDrive"
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried doing it without the .class and got more errors.
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, nevermind I think I got it after the CLASSPATH errors I was receiving earlier. I just tried javac SimpleDotComTestDrive. Generally the Windows CLI is not case sensitive, however, I guess the JVM is. After the CLASSPATH fix and adjusting my Case Sensitivity, it works as intended. I would have never caught the CPATH var without the help! I know I'm asking a bit much, but could anyone explain why the CLASSPATH variable needs the ".;" at the beginning? It's just for self education in case I run into these problems again. Thank you all very much for all of the help!
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why on earth are you going to the bin folder? Don't work from there.

The "wrong name" error message suggests you have got a package name in one of the files and are not calling it by its full-qualified name. Or you have misspelled it; I think there ought to be a few capital letters in the invocation. Use the "dir" command to find where all your .class files are.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need the capital letters. "java SimpleDotComTestDrive"
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Smallman wrote: . . . geniusness . . .
Not at all (aw, shucks!). Just lots of practice.
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The . (Period) represents your current directory and the ; (semicolon) separates it from the next entry.
 
Keitaro Oman
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all very much! You've all been great help! I hope I can contribute to this site as well, once I get more of a grasp on Java ^^
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic