From my home directory, I created a directory pkg and wrote 2 files A.java and B.java in it.

A.java

package pkg;
class A
{
	B b;
}

B.java

package pkg;
class B
{
}

I went back to my home directory and typed the following :

javac -classpath . /pkg/A.java

It compiled correctly. My question is that A.java has a reference to class B. The compiler searches the classpath for any packages that contains class B. pkg being the only package in the classpath, it doesn't find the B class file there. It should have been the end of story and the code shouldn't have compiled. But what happens is that the B.java file is also compiled. Why ?

Because the compiler you used is setup to search the source directory, as well as the classpath, and compile those items from the src dir if they are newer than the current class versions.

Sorry, I don't understand. The source directory (where the 2 files are placed) is pkg. There is no class file initially in the source directory, so what is meant by "current class version".

In that case the "current class version" would be "none", would it not. But how I really should have said it is "the date time stamp of the current compiled classfile associated with that source file".

And it wouldn't be found in the "source directory", it would be found "on the classpath". Now, that might, and might not, be the same place, depending on the setup.

This is what I get. Since I am not specifying the sourcepath option with javac, the following command will search for the class B in the classpath or the corresponding file B.java (so that it can compile it to produce B.java) also in the classpath.

javac -classpath . pkg/A.java

The current directory is my home directory. It first looks for individual B.class or B.java files in the current directory, then it goes into the sub-directories. In the sub-directory pkg, it finds the file B.java. It compiles it to produce B.class and utilizes it.

Is this really what happens ?

Strangely, if I move the file B.java from the directory pkg and place it in the current directory, then it can't seem to find this B.java file to compile it to produce B.class ? Why is this happening since the docs say that the default sourcepath is the classpath and I am placing the file B.java in my classpath. ?

Stranger still, if I move the A.java file to the current directory and compile it from there, it seems to find B.java !

Even if I remove the package declaration from B.java, the compiler still can't find it though its present in my current directory (home folder) and I have set my classpath to the current directory. Why is it that B.java is only found when I place it in the pkg folder ?

Even if I remove the package declaration from B.java

Then B becomes part of the unnamed namespace and types belonging to the unnamed namespace are invisible to namespaced types starting Java 1.4 since there is no mechanism wherein you can import those. Your example should work if you also move the class A outside the 'pkg' directory (i.e. remove the package declaration).

Your example should work if you also move the class A outside the 'pkg' directory (i.e. remove the package declaration).

Thanks a lot. My example worked as you said. But if I go into the pkg directory and type

javac -cp . A.java

, it is not able to find class B.I have removed all the package declarations from the files.. The unnamed namespace is no longer the one that contains B.java (as B.java is in the home folder and I have moved from my home folder to the pkg folder and am compiling from there, so the unnamed namespace is the pkg folder now)

Also, when both the files (with package declarations intact) were in the pkg folder and I was compiling A.java from the home folder with classpath set to the current directory, why was the compiler able to find B.java in the pkg directory. Does it check the package mentioned in the source file for classes too, along with the classpath ?

, it is not able to find class B.I have removed all the package declarations from the files.. The unnamed namespace is no longer the one that contains B.java (as B.java is in the home folder and I have moved from my home folder to the pkg folder and am compiling from there, so the unnamed namespace is the pkg folder now)

I'm not sure why that is the case since in the end both are unnamespaced classes. But trying out the same locally works for me so it must be something at your end. Double check the file contents.

Also, when both the files (with package declarations intact) were in the pkg folder and I was compiling A.java from the home folder with classpath set to the current directory, why was the compiler able to find B.java in the pkg directory. Does it check the package mentioned in the source file for classes too, along with the classpath ?

AFAIK, it follows it's own custom dependency check algorithm which really shouldn't concern you since it's not part of any specification and can vary from implementation to implementation.

Edited 6 Years Ago by ~s.o.s~: n/a

Thanks. You were right. It was my fault. I was mentioning the present directory in the classpath when I actually meant the parent directory to be searched.

Thanks a lot to everyone who contributed.

You are welcome. I see that this thread was not "marked as solved" even though you found a solution to your original question (which I've done it for you for now). Please ensure that the threads you create are "marked as solved" if a resolution has been reached for the original problem. This might actually help those who search for only solved threads when searching for answers to questions which have been previously asked on this forum.

This question has already been answered. Start a new discussion instead.