File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import on demand

 
Dinesh Kumar
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dinesh Kumar
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanx
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
Thanx.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic