"cannot find symbol" error message
I tried compiling a program that instantiates an object of another class from a separate .java file. However, both .java files are in the same directory. I've never encountered this problem before, and I know some path variable must be wrong somewhere but I don't where. I also tried added a main method to the class I was trying to instantiate an object of ( from the separate .java file ), and then successfully compiled that separate file, but when I try to run it I get a runtime exception that says: "Exception in thread "main" java.lang.NoClassDefFoundError". I know the problem is not in the program itself because the entire program compiled and ran seemingly without any kind of error on a different computer, so there must be some sort of problem with my computer. Thanks in advance for any help.
[815 byte] By [
@modia] at [2007-10-2 1:40:19]

This is usually a classpath problem. Can u check both your compiled java files (.class ) are in the classpath or in the directory where you call the program?
All of the .java files I was trying to compile are in the same directory. I'm pretty sure its a problem with the compiler, because a different set of .java files which compiled and ran fine on my computer are not compiling now, and I have not touched the directory they are in since I last compiled them.
I'm not entirely sure how to check the classpath, but, if it's checked the way I think, it's "C:\Program Files\Java\jre1.5.0_04\lib\ext\QTJava.zip". For future reference, so I can be certain, how do you check the classpath?
@modia at 2007-7-15 19:03:55 >

Usually there is a environment variable which is called CLASSPATH. You can find it out by typing "set" in a command box if you're using windows. But because the classpath may differ dependent on the project you can set it manually when compiling and running the project
e.g.
javac -cp "C:\Program Files\Java\jre1.5.0_04\lib\ext\QTJava.zip" <Class>.java
same for java
All classes you reference should be in the classpath, either directly or contained in a library
Ok, I checked it through System Properties and got the same thing that the set command gave. I also tried specifying the classpath on the command line with -cp switch but its still not compiling.
@modia at 2007-7-15 19:03:55 >

All the "cannot find symbol" error messages reference classes from files in the same directory as the one I'm trying to compile directly, and they all refer to attempts to instantiate objects of those classes.
@modia at 2007-7-15 19:03:55 >

Are any of the classes in a named package? That is, does the source code start with "package mypackage;"
You might try javac -cp.TheClass.javaIf that does not work, please post the command you used, at least one of the full exact error messages and tell us whether the classes are in a named package.
None of the source code files are in a package. Even so, when I specified the classpath option as "." it compiled, but when I tried to run it, it threw a runtime exception. Here are the exact error messages:
//with classpath:
>javac -cp . TicTacToe.java
>java TicTacToe
Exception in thread "main" java.lang.NoClassDefFoundError: TicTacToe
//without classpath:
>javac TicTacToe.java
TicTacToe.java:34: cannot find symbol
symbol : class Player
location: class TicTacToe
private static Player[] player = new Player[ 2 ];
^
TicTacToe.java:7: cannot find symbol
symbol : class Human
location: class TicTacToe
player[ 0 ] = new Human();
//...many more messages like these for other classes used in the program
@modia at 2007-7-15 19:03:55 >

> >javac -cp . TicTacToe.java
> >java TicTacToe
> Exception in thread "main"
> java.lang.NoClassDefFoundError: TicTacToe
>
You probably need to use java -cp . TicTacToeIf that does not work, are you sure that this was the full error message? Was there additional text that said, "wrong name: ....."? After compiling, did a file named TicTacToe.class get created in the current directory?
I tried it again and got this message the first time I did it:
Exception in thread "main" java.lang.NoClassDefFoundError: TicTacToe/java
Then I tried it again and got this:
Exception in thread "main" java.lang.NoClassDefFoundError: TicTacToe
the only difference being the "/java" at the end.
Then I specified the classpath and it ran.
I believe I specified the classpath when I got the first error message but I'm not 100% sure because when I ran the program, it produced enough text to use the entire screen and I couldn't scroll back and see what was there before.
@modia at 2007-7-15 19:03:55 >

> Then I specified the classpath and it ran.
>
As expected.
If you need to specify the classpath when compiling, you likely will need to when running. Classpath is how javac.exe and java.exe find classes.
Many people in these forums recommend always using the -classpath option because as your projects get more numerous and complex, you will probably need different classpaths for each project.
Thanks for all your efforts, now I know my compiler at least works even if it doesn't work well, but aren't the compiler and the runtime environment both supposed to check the current directory? I know before when I compiled and ran programs I never had to add the current directory to the classpath. Do you know what might have caused the change?
@modia at 2007-7-15 19:03:55 >

Possibly you had "." contained in the environment classpath variable and compiled without setting the classpath explicitly. Then the environment variable changed for some reason and that caused the problems.
A decent IDE really helps handling the classpath and does many more usefull things. I for example use eclipse, but there are a lot of good ones, depends on your taste which IDE you like. When you've got some time I suggest you try some out.
> Thanks for all your efforts, now I know my compiler
> at least works even if it doesn't work well, but
> aren't the compiler and the runtime environment both
> supposed to check the current directory?
If your system has no Classpath environmental variable set, then they do default to the current directory. If your system has a Classpath environmental variable set, then the directories in the variable are searched. The current directory (.) must be in the Classpath if you want it to be searched.
>I know
> before when I compiled and ran programs I never had
> to add the current directory to the classpath. Do you
> know what might have caused the change?
If you enter "set" or "set Classpath" in the comand prompt, you should see the setting of your system's Classpath. That should give you a clue as to what changed. I have seen cases where installing or upgrading QuickTime has modified the Classpath. (I am assuming a Windows system.)
> I have seen cases where installing or
> upgrading QuickTime has modified the Classpath. (I
> am assuming a Windows system.)
The system on which the code failed to compile is a Windows XP machine, and I did recently install QuickTime on it. Installing QuickTime must have changed the classpath. Thank you both for helping me, this could potentially have been a very confusing and frustrating process. One final question I have is how do I set this post as answered so Duke, the java mascot, shows up on the forums page as an attribute of the topic (I'm assuming the Duke icon means an answered question)?
@modia at 2007-7-15 19:03:55 >

The Duke Dollars are a way to reward people for proving answers or at least something valuable to a poster's question. They do not mean a problem was or was not resolved. I have never been concerned about them, but I think you have to include the dollar value when you first start a new topic.
It is very good for people who asked for help to post how the problem was solved, as you did. Many posters do not. It is also much appreciated when you thank people for help.
Thanks for your hint about QuickTime.
I have experienced the same problem where javac was working properly and then suddenly one day it didn't.
After reading your post, I uninstalling QuickTime. Javac successfully compiled the same program that had failed just prior to uninstalling QuickTime. Before un-installing QuickTime, I found data in the Windows registry that referenced non-existent folders in C:\ProgramFiles\java.
I know some other developers who are experiencing the same issue and will pass this suggestion on to them. Thanks again for posting this info.
Try un-installing QuickTime. That resolved the issue for me.
> Try un-installing QuickTime. That resolved the issue
> for me.
Well, if you have a need to run QuickTime, removing it is not a good solution for a classpath problem. It is not too much trouble to use -classpath in the compiling and running process, or you can change the system classpath without impacting QuickTime.