• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot find symbol error on compile, (and I tried to keep this really simple)  RSS feed

 
Craig Freeman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just starting w/Java. I got the path & classpath thing straight and have successfully created and compiled several simple classes. I'm having trouble with a "cannot find symbol : class Test1" error when compiling a class that references anything related to Test1.class. It's not just related to Test1, I've copied other examples from a textbook, and I can't compile anything that references another class (at least how I'm trying to compile).

Everything is in C:\javwrk, that's the complete path with the following present:

Test1 JAVA
Test1.class (compiled ok)
Test1Driver JAVA (references Test1)

The error occurs when I try to compile Test1Driver. I'm doing this from a command line (cmd prompt).



cd c:\
c:\>javac javwrk\Test1.java (this compiles ok)

c:\>javac javwrk\Test1Driver.java (this gets the "cannot find symbol : class Test1" error)

Again, I've tried coding other examples from a textbook, and I get the same error when I try to compile one class referencing another class in any way.

Thanks in advance for help.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Freeman wrote:...I got the path & classpath thing straight...

Welcome to the Ranch!

I'm not convinced about the classpath. Did you set a system/user classpath? If so, why? You're probably best without one.

Your best approach is probably to change the current directory to javwrk using the cd (change directory) command...

C:\>cd javwrk
C:\javwrk>

Then compile from that directory (since Java's default is to look for classes in the current directory)...

C:\javwrk>javac Test1Driver.java

(If that doesn't work, then you probably have a classpath issue caused by setting a system/user classpath.)

Alternatively, you could compile from C: and supply a command line classpath (-cp) of javwrk...

C:\>javac -cp javwrk javwrk\Test1Driver.java
 
zameel amjed
Greenhorn
Posts: 14
Notepad PHP Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to diclare the path correctly
then try to compile by diving to the root directry where the .java file saved. note that the java is case sensitive and besure to type correct file name.

this problem also can occur because of saving the file in different name than mentioned in public class. save the file in same name which you have given to the public class.


save this file as A.java

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to have answered errors the original poster is not suffering, zameel amjed.
 
Craig Freeman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Editing this to correct some things.]

Marc and all, thanks for getting back to me.

I originally set the Class and Classpath in system/user pointing to the jdk\bin directory because javac compile command didn't run, got an error saying javac command wasn't found, or something to that effect.

I have also tried :

cd c:\javwrk

c:\javwrk>javac Test1Driver.java and still get the same error.


I've also tried c:\>javac -cp . javwrk\Test1Driver.java - and still got the same error

All of the above work in compiling Test1, but in compiling Test1Driver, I get the same error about not being able to find Test1.

I guess removing the class path entry from system/user is the next step.



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Freeman wrote:I originally set the Class and Classpath in system/user pointing to the jdk\bin directory because javac compile command didn't run, got an error saying javac command wasn't found, or something to that effect.

You have to add the jdk\bin directory to the PATH to get rid of that error, so that's OK. But do not add the jdk\bin directory to the CLASSPATH. The CLASSPATH is where Java looks for *.class files, and those are not in the jdk\bin directory.

It's best to not set the CLASSPATH environment variable at all.



Craig Freeman wrote:
I have also tried :

cd c:\javwrk

c:\javwrk>javac Test1Driver.java and still get the same error.


I've also tried c:\>javac -cp . javwrk\Test1Driver.java - and still got the same error

You have to include the directory that contains the *.class file in the classpath - not the source file itself. Try this:

cd c:\javwrk

c:\javwrk>javac -cp . Test1Driver.java

The "-cp" sets the classpath for the javac command, and "." means "the current directory".
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig Freeman wrote:...I originally set the Class and Classpath in system/user pointing to the jdk\bin directory because javac compile command didn't run, got an error saying javac command wasn't found...

Updating the path variable will fix that. That's what tells your system where to look when you enter a command like "javac" from any directory. The classpath does not affect this.

See Java Tutorial - Path and Classpath for clarification.

Craig Freeman wrote:...I have also tried :

cd c:\javwrk

c:\javwrk>javac Test1Driver.java and still get the same error...

That suggests you have a system/user classpath set, and it does not contain a dot for the current directory (javwrk).

Craig Freeman wrote:...I've also tried c:\>javac -cp . javwrk\Test1Driver.java - and still got the same error...

Here, your current directory is "C" again, and you are specifying a classpath of the current directory (C). That will not work, because the class you want is in javwrk. If your current directory is C, then you could do this...

C:\>javac -cp javwrk javwrk\Test1Driver.java

Craig Freeman wrote:...I guess removing the class path entry from system/user is the next step...

That's the best plan.

From the tutorial link above, "The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value." So what you've done by setting a system/user classpath is to override Java's default of using the current directory. So after removing your system/user classpath, you should be able to compile from C:\javwrk without supplying any classpath, because the default of "." will be used...

C:\>cd javwrk
C:\javwrk>javac Test1Driver.java
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!