Ravishankar
Ravishankar

Reputation: 91

Java compiler behaviour question

I have an interface & its implementation class in the same package. I using javac in commandline to compile them. I am able to compile the interface class successfully, but when try to compile the implementation class after compiling the interface class, I get the error - Symbol not found. However, as both the interface & its implementation are in the same folder, if I do a Javac *. I am able to compile both of them .

Can someone help me understand this behaviour ? Thanks for your time

Upvotes: 1

Views: 165

Answers (2)

rsp
rsp

Reputation: 23373

The Java compiler looks for packages by way of a file name convention, a.b.c.Interface is interpreted as look for the interface in a/b/c/Interface.class

If you run javac from the root of your classpath, the compiler will find your interface. I.e. If your package is at D:\sources\a\b\c, start javac in D:\sources and compile a\b\c\Impl.java

Upvotes: 5

Aaron Digulla
Aaron Digulla

Reputation: 328556

You must compile both files at the same time or add the .class file of the first run in the classpath of javac. javac doesn't try to be smart and doesn't search your harddisk for files which might resolve missing symbols. If you don't pass something in, it won't find it.

Upvotes: 1

Related Questions