This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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...
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
Joined: Aug 26, 2001
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 ]
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 )
Joined: Jul 03, 2002
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.