• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80273
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you really have a / before pkg in that compiler instruction?
 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80273
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome To JavaRanch
 
Daud Miyan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80273
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic