• Post Reply Bookmark Topic Watch Topic
  • New Topic

I got a magic experience about java classpath  RSS feed

 
Mellon Sun
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I write two class.
Outer.java
public class Outer{
}
Test.java
package hd;
public class Test{
Outer out;
}
I put them two in the same directory "C:\mellon\hd".When I use javac in command line, something strange happened.
look:
C:\mellon>dir hd
2003-12-19 09:49 <DIR> .
2003-12-19 09:49 <DIR> ..
2003-12-19 09:32 22 Outer.java
2003-12-19 09:32 52 Test.java
C:\mellon>javac hd/Test.java
C:\mellon>dir hd
2003-12-19 09:49 <DIR> .
2003-12-19 09:49 <DIR> ..
2003-12-19 09:49 184 Outer.class
2003-12-19 09:32 22 Outer.java
2003-12-19 09:49 212 Test.class
2003-12-19 09:32 52 Test.java
You see ,I compiled the two java file and got two class.Then I deleted them.
C:\mellon>del hd\*.class
C:\mellon>dir hd
2003-12-19 09:50 <DIR> .
2003-12-19 09:50 <DIR> ..
2003-12-19 09:32 22 Outer.java
2003-12-19 09:32 52 Test.java
I tried to compile them in another way.
C:\mellon>javac hd/Outer.java
C:\mellon>javac hd/Test.java
hd/Test.java:3: cannot access hd.Outer
bad class file: .\hd\Outer.class
class file contains wrong class: Outer
Please remove or make sure it appears in the correct subdirectory of the classpath.
Outer out;
^
1 error
So ,if I first compile the Outer.java which without package declared ,I can get Outer.class.
But I can not get Test.class. Why?
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know what, I'm willing to bet that after you compile your Outer.java, the Outer.class is in your C:\mellon directory instead of C:\mellon\hd.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that Java is exacting regarding the relationship between file path and package name. Your "Outer" class isn't in a package, so you must put the "Outer.java" file in the C:\mellon directory. The "Test" class is in the "hd" package, so you must put the "Test.java" find in the "C:\mellon\hd" directory.
Then you can compile and run the classes. But make sure that you put the "C:\mellon" directory in your class path.
[ December 19, 2003: Message edited by: Wayne L Johnson ]
 
Mellon Sun
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Adrian Yan:
1.
I put the two java file in the same directory "c:\mellon\hd",and one has package declared "package hd;" but another has not.
2.
I did not set the classpath with the directory c:\mellon.
3.
I can compile the two java file in command line with command "javac hd\Test.java", and the two generated class file are in the same directory "c:\mellon\hd".
4.
If I use "javac hd\*.java" ,the compiler wile complain.
I can first compile the "c:\mellon\hd\Outer.java" with "javac hd\Outer.java" and get "c:\mellon\hd\Outer.class". But I can't comiple the "c:\mellon\hd\Test.java" which has package declared after the Outer.java compiled.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!