File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion 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 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 in the "my" directory.
Your directory should look like this:

Then you can go to the directory "my" and compile using the following command:
root/my$> javac
Then go to the root directory and type:
root$> javac -classpath .
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 in the root directory and only in "my" directory, and then compile as you said, both work fine.
But if I keep an additional copy of (uncompiled) in the root folder as well then stops compiling. Note that another copy of 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 compiled fine when I removed 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 stay in the same directory as, 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 from B's directory, in clear just remove 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, 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:
subject: import on demand
It's not a secret anymore!