• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot find symbol error during compilation

 
Mudit Handa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have two files 1) MyClass.java 2)Another.java

Directory Structure D:\chap10\com\foo

Both files are in foo folder

MyClass.java contain following code

package com.foo;
public class MyClass
{
public void hi() {System.out.println("hi?");}
}

Another.java contain following code

package com.foo;
import com.foo.*;
public class Another {
void go() {
MyClass m1 = new MyClass();
com.foo.MyClass m2 = new com.foo.MyClass();
m1.hi();
m2.hi();
}

public static void main(String [] args)
{
Another an = new Another();
an.go();
}
}

Following error occur when Another.java is compiled

D:\ExamPractice\com\foo>javac Another.java
Another.java:9: cannot find symbol
symbol : class MyClass
location: class com.foo.Another
MyClass m1 = new MyClass();
^
Another.java:9: cannot find symbol
symbol : class MyClass
location: class com.foo.Another
MyClass m1 = new MyClass();
^
Another.java:10: cannot find symbol
symbol : class MyClass
location: class com.foo.Another
MyClass m2 = new MyClass();
^
Another.java:10: cannot find symbol
symbol : class MyClass
location: class com.foo.Another
MyClass m2 = new MyClass();
^
4 errors

Refer to example on page 769 SCJP study guide by Kathy Sierra & Bert Bates
 
Henry Wong
author
Marshal
Pie
Posts: 21430
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your classpath is likely not set... trying compiling as so...

D:\ExamPractice>javac com\foo\Another.java


Henry
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any need to for the import statement when both the classes are in the same package?
wont compiler give error. I use eclipse and it just dont let me write this kind of import statement.
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes in the same package we don't need import statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic