File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Test class cannot find other class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Test class cannot find other class" Watch "Test class cannot find other class" New topic
Author

Test class cannot find other class

Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
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

Joined: Nov 04, 2009
Posts: 2066

What is you directory structure?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
RIght now all I have is a directory named SimpleDotCom with both of the above .java files in it.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Did you check the Java file names? And how do you run the program?
Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
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

Joined: Jan 05, 2010
Posts: 64
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


Don't walk as if you rule the world, walk as if you don't care who rules it...
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Oh and Welcome to Java Ranch!
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
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!


Everyday in every way, we get a little better.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
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

Joined: Sep 09, 2010
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38353
    
  23
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

Joined: Sep 09, 2010
Posts: 107
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

Joined: Jan 05, 2010
Posts: 64
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

Joined: Sep 09, 2010
Posts: 8
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

Joined: Sep 09, 2010
Posts: 107
Just type "java SimpleDotComTestDrive"
Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
I tried doing it without the .class and got more errors.
Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38353
    
  23
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

Joined: Sep 09, 2010
Posts: 107
You need the capital letters. "java SimpleDotComTestDrive"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Bobby Smallman wrote: . . . geniusness . . .
Not at all (aw, shucks!). Just lots of practice.
Bobby Smallman
Ranch Hand

Joined: Sep 09, 2010
Posts: 107
The . (Period) represents your current directory and the ; (semicolon) separates it from the next entry.
Keitaro Oman
Greenhorn

Joined: Sep 09, 2010
Posts: 8
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

Joined: Oct 13, 2005
Posts: 38353
    
  23
You're welcome
 
jQuery in Action, 2nd edition
 
subject: Test class cannot find other class