| Author |
Compiling a file that has reference to a class contained in another UNCOMPILED file.
|
Daud Miyan
Greenhorn
Joined: Jul 07, 2010
Posts: 23
|
|
From my home directory, I created a directory pkg and wrote 2 files A.java and B.java in it.
A.java
B.java
I went back to my home directory and typed the following :
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 ?
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
Daud Miyan wrote:
pkg being the only package in the classpath, it doesn't find the B class file there.
How did you tell this?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Daud Miyan
Greenhorn
Joined: Jul 07, 2010
Posts: 23
|
|
"pkg being the only package in the classpath, it doesn't find the B class file there. "
The classpath is the current directory. The only sub-directory that has java files or class files is pkg
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Did you really have a / before pkg in that compiler instruction?
|
 |
Daud Miyan
Greenhorn
Joined: Jul 07, 2010
Posts: 23
|
|
|
Yes. If I hadn't, the compiler would not have been able to find A.java which is in the pkg folder. pkg has both A.java and B.java. The problem is that since A.java references class B and class B doesn't exist as of now, the compilation should have failed. But the compilation is successful and the file B.class gets created as a side effect of compiling A.java. How is this happening ?
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Oh, your problem is, why does the compiler compile class B even though you haven't explicitly compiled the Class B before Class A? Correct?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
|
Obviously what confused me about the initial / is that I usually use *nix where an initial / will take you to the root of the drive. The syntax of the / must be different here. Sorry for any confusion.
|
 |
Daud Miyan
Greenhorn
Joined: Jul 07, 2010
Posts: 23
|
|
Abimaran Kugathasan wrote:Oh, your problem is, why does the compiler compile class B even though you haven't explicitly compiled the Class B before Class A? Correct?
Right. And how does it find out which file to compile in order to generate class B ? It must be going through this entire process when it encounters a reference to class B in A.java and doesn't find B.class in the classpath. In fact, why doesn't it just stop here when it can't find class B in the classpath, like it usually does and throw a compile-time error? Why compile B.java ?
Campbell Ritchie wrote:Obviously what confused me about the initial / is that I usually use *nix where an initial / will take you to the root of the drive. The syntax of the / must be different here.
I am really sorry !! I forgot to include ~ before /pkg to denote my current directory which is the home directory. But I made the typing error on this forum only. On the terminal I typed without the / which is equivalent to ~/ in my case. I totally missed it. Sorry again. I am using Linux too.
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Daud Miyan wrote:pkg being the only package in the classpath, it doesn't find the B class file there.
did you see both the classes are in same package,why not you try to shift the class B to different package say pkg2 and then see the result.
|
SCJP6.0,My blog Ranchers from Delhi
|
 |
Shanky Sohar
Ranch Hand
Joined: Mar 17, 2010
Posts: 1046
|
|
Welcome To JavaRanch
|
 |
Daud Miyan
Greenhorn
Joined: Jul 07, 2010
Posts: 23
|
|
Shanky Sohar wrote:
Daud Miyan wrote:pkg being the only package in the classpath, it doesn't find the B class file there.
did you see both the classes are in same package,why not you try to shift the class B to different package say pkg2 and then see the result.
There is no class A or class B at the time I am compiling A.java. On compilation, both are appropriately generated in the package pkg. My question is about the mechanism of generation of these class files considering the fact that class B is not present at the time of compiling class A. By what mechanism does javac cause B.java to be implicitly compiled on compiling A.java ?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
|
|
Daud Miyan wrote: . . . I am really sorry !! I forgot to include ~ before /pkg . . .
Apology accepted
Use copy-and-paste; that will obviate such confusion.
We have other hints about copying text: start from this FAQ.
|
 |
 |
|
|
subject: Compiling a file that has reference to a class contained in another UNCOMPILED file.
|
|
|