File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling a file that has reference to a class contained in another UNCOMPILED file.

 
Daud Miyan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Daud Miyan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you really have a / before pkg in that compiler instruction?
 
Daud Miyan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome To JavaRanch
 
Daud Miyan
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic