wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes import on demand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "import on demand" Watch "import on demand" New topic

import on demand

Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
I picked up this code from an earlier post, where it has not been satisfactorily answered.

The problem is at (1). If this import is replaced by "import my.A" then this code for B.java compiles otherwise it does not.
The JLS says this

The example:
import java.util.*;
causes the simple names of all public types declared in the package java.util to be available within the class and interface declarations of the compilation unit....

Am I missing something?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Be sure to save file A.java in the "my" directory.
Your directory should look like this:

Then you can go to the directory "my" and compile A.java using the following command:
root/my$> javac A.java
Then go to the root directory and type:
root$> javac -classpath . B.java
Then you can execute the program with:
root$> java -classpath . B
And that should work (at least I had the priviledge to see "HI" printed on the screen )
Let us know if you still have some problems...

[Blog] [Blogroll] [My Reviews] My Linked In
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Thanks Valentine, I discovered something strange.
If I keep B.java in the root directory and A.java only in "my" directory, and then compile as you said, both work fine.
But if I keep an additional copy of A.java (uncompiled) in the root folder as well then B.java stops compiling. Note that another copy of A.java and a compiled A.class is already there in the directory my.
I am writing down what the screen showed to me in the code tags below. I was compiling this on the command prompt of a Windows based system

As you can see B.java compiled fine when I removed A.java from the root folder.
Can you pls. explain this?
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
That's because of the unqualified access to class A in the following statement:
A a = new A();
When class B is compiled, the above statement is encountered and a class called A is searched in B's package. If none is found, the class A is searched in the imported package "my". But the thing is, that if you let A.java stay in the same directory as B.java, the compiler will take it and compile it. A compile error will follow since class A is declared to be in package "my" but isn't...
Try replacing the above statement by:
my.A a = new my.A();
and you'll see that the compilation succeeds...
But the qualified access is unneeded if the import statement is included. Thus, I advise to keep the import statement, keep the unqualified statement and remove the file A.java from B's directory, in clear just remove A.java from the root directory and that's it...
[ July 30, 2002: Message edited by: Valentin Crettaz ]
David Poglitsch

Joined: Jul 26, 2002
Posts: 16
Also important not to have another subdirectory out there called "B" in your root directory when compiling B.java, or you'll get the following:

(Just happened to have that spare folder laying around )
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Thanks Valentin & David.
Just another minor discovery on David's suggestion -
If there's a directory named "B" in the root directory but it is kept empty, then there's no error. The error appears if there's a .java or a .class file kept in "B".
I agree. Here's the link: http://aspose.com/file-tools
subject: import on demand
It's not a secret anymore!