• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting cannot find symbol error  RSS feed

 
Ajay Varma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, kindly help me with the following issue.
I am trying to complile a simple code as below.



When I compile class A using command C:\Users\Ajay\Documents\java_sandbox>javac A.java ....... it compiles fine
But when I compile Class B using the same C:\Users\Ajay\Documents\java_sandbox>javac B.java it gives the error as below....

C:\Users\Ajay\Documents\java_sandbox>javac B.java
B.java:2: error: cannot find symbol
class B extends A {
^
symbol: class A
B.java:8: error: cannot find symbol
System.out.println("The sum of i, j, k : "+(i+j+k));
^
symbol: variable i
location: class B
B.java:8: error: cannot find symbol
System.out.println("The sum of i, j, k : "+(i+j+k));
^
symbol: variable j
location: class B
3 errors


However when I compile the same code in Eclipse IDE , it works fine with no error at all. But the command prompt gives all these errors.I just don't want to touch the IDE until I complete the Core java part.
I have recently started studying Java and I am not able to compile any java file which uses extends in other words which uses Inheritance feature. Any help will be appreciated. Thanks in advance
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you use windows,
*probably* you would have been added *wrong class path in your environment variable*. so when you compile without -cp option compiler picks from the existing class path value from environment variable. so just try[override class path] javac with -cp option as in *javac -cp . B.java* .here priod operator means current working directory as class path.

so that, it is better not to set class path variable in environment variable option.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to JavaRanch Ajay
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ajay.........As you said when you are compiling the B.java file you are getting errors..Now you are getting errors because when you are compiling the B.java file the compiler is not getting the A.class file (in simple words the compiler has no idea that there is a file "A")........This is the problem of CLASSPATH...........i.e. you need to set CLASSPATH.......So before compiling the B.java file set CLASSPATH in your cmd like this............... set CLASSPATH=C:\Users\Ajay\Documents\java_sandbox; (considering that you are storing files in java_sandbox folder)
........................Now if you are not interested in setting the CLASSPATH in your cmd then other way of dealing with this error is that you compile your program like this
javac -cp . B.java (i.e) javac [space] -cp [space] dot [space] filename.java
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

If you are a real beginner you are better creating your own "java" folder and putting your classes in that.
Print out your classpath and see how much of it you have created yourself. If 100%, then get rid of the system classpath; that usually does more harm than good.
 
Javin Paul
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a new member of Java family, I would suggest to spend some time to learn path and classpath in Java , because most of the time these error will take lot of your time if you don't understand there behavior, specially classpath. I had wasted lot of time with these kind of issue while learning Java

By the way you may find my blog post How Classpath works in java interesting
 
Ajay Varma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much everyone, both the methods suggested by Seetharaman and Rameshwar Soni worked perfectly. Now my java files are compiling perfectly. Thank you for your help guys and thank you everyone for your warm welcome. This is the best forum I have come across so far.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!