Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Frustrating compilation "Cannot resolve symbol" error

 
Manavendra Chimakurthi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I have gone through a bunch of classpath related messages before posting this. I appreciate your help in this very much.
Here are two classes I have:
class FooBar
{
   public String toString(){
   return new String("FooBar lives");
   }
}
class Foo
{
   public static void main(String args[]){
      System.out.println("Foo lives");
      FooBar foobar = new FooBar();
      System.out.println(foobar);
   }
}
If I have both classes in the same file. I can compile Foo.java (javac Foo.java) and run Foo (java Foo) with expected results:
Foo lives
FooBar lives
If I have each class in its own file. I can compile FooBar (javac FooBar.java), but when I try to compile Foo (javac Foo.java), I get a "Cannot resolve symbol" error for the FooBar class.
Here is the error transcript:
Foo.java:5: cannot resolve symbol
symbol: class FooBar
location: class Foo
FooBar foobar = new FooBar();
^
Foo.java:5: cannot resolve symbol
symbol: class FooBar
location: class Foo
FooBar foobar = new FooBar()
                             ^
2 errors

I don't understand where the problem is. Classes in the current directory are available without having to set the classpath explicitly - right. I ran javac using the -classpath option but still got the same error. Here are my environment variable settings:
CLASSPATH: C:\Program Files\Altova\xmlspy\XMLSpyInterface.jar;C:\Program Files\junit3.8.1\junit.jar;C:\Program Files\junit3.8.1;
JAVA_HOME: C:\Program Files\j2sdk_nb\j2sdk1.4.2
PATH: %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\PROGRA~1\Tcl\bin;C:\Program Files\j2sdk_nb\j2sdk1.4.2\bin;C:\Program Files\Apache Group\apache-ant-1.5.4\bin;
[ September 04, 2003: Message edited by: Manavendra Chimakurthi ]
[ September 04, 2003: Message edited by: Manavendra Chimakurthi ]
[ September 04, 2003: Message edited by: Manavendra Chimakurthi ]
[ September 04, 2003: Message edited by: Manavendra Chimakurthi ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default current directory is included in the classpath.
Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.
You have overriden the default classpath, so use
SET CLASSPATH=%CLASSPATH%;.


[ September 04, 2003: Message edited by: Pradeep Bhat ]
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add the current directory (.) to the classpath:
CLASSPATH: C:\Program Files\Altova\xmlspy\XMLSpyInterface.jar;C:\Program Files\junit3.8.1\junit.jar;C:\Program Files\junit3.8.1;.;
I re-created the problem by taking "." out of my classpath, so I'm pretty sure that's what your problem is.
 
Manavendra Chimakurthi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. That worked. But why did it work? Aren't the classes in the current directory available to other classes by default?
 
Manavendra Chimakurthi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pradeep and Wayne. I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic