这几天碰到了编译失败的严重问题,所以只好痛下心来好好研究看看:
我的OS:Windows XP
有设PATH:C:Program FilesJavajdk1.6.0_07bin
package要用的程式码Fish.java:
package water;
public class Fish {
public static String name = “小金”;
public static String type = “金鱼”;
public static String color = “金”;
public static void skill() {
System.out.println(“吐泡泡”);
}
}
import 要用的程式码PetStore3.java:
import water.*;
public class PetStore3 {
public static void main(String[] args) {
String fishName = Fish.name;
String fishKind = Fish.type;
String fishColor = Fish.color;
System.out.println(“我有一只优雅的” + fishKind +
“, 名叫” + fishName +
“, 他的颜色是”+fishColor+” 色的.”);
System.out.println(“每当他肚子饿的时候都会”);
Fish.skill();
}
}
分14种状况测试,这真的很好玩,结果有令人恍然大悟的感觉:
| Fish.java位置 | classpath设定 | PetStore3.java位置 | 结果 |
| c:\water | .; | c:\ | OK |
| c:\water | .; | c:\water | PetStore3.java:1: package water does not exist import water.*; ^ PetStore3.java:5: cannot access Fish bad class file: .Fish.class class file contains wrong class: water.Fish Please remove or make sure it appears in the correct subdirectory of the classpa th. String fishName = Fish.name; ^ 2 errors |
| c:\water | .;c:\; | c:\water | PetStore3.java:5: cannot access Fish bad class file: d:sourcejavawaterFish.class class file contains wrong class: water.Fish Please remove or make sure it appears in the correct subdirectory of the classpa th. String fishName = Fish.name; ^ 1 error |
| c:\water | .;c:\; | c\: | OK |
| c:\source\water | .; | c:\ | PetStore3.java:1: package water does not exist import water.*; ^ PetStore3.java:5: cannot find symbol symbol : variable Fish location: class PetStore3 String fishName = Fish.name; ^ PetStore3.java:6: cannot find symbol symbol : variable Fish location: class PetStore3 String fishKind = Fish.type; ^ PetStore3.java:7: cannot find symbol symbol : variable Fish location: class PetStore3 String fishColor = Fish.color; ^ PetStore3.java:12: cannot find symbol symbol : variable Fish location: class PetStore3 Fish.skill(); ^ 5 errors |
| c:\source\water | .;c:\; | c:\ | PetStore3.java:1: package water does not exist import water.*; ^ PetStore3.java:5: cannot find symbol symbol : variable Fish location: class PetStore3 String fishName = Fish.name; ^ PetStore3.java:6: cannot find symbol symbol : variable Fish location: class PetStore3 String fishKind = Fish.type; ^ PetStore3.java:7: cannot find symbol symbol : variable Fish location: class PetStore3 String fishColor = Fish.color; ^ PetStore3.java:12: cannot find symbol symbol : variable Fish location: class PetStore3 Fish.skill(); ^ 5 errors |
| c:\source\water | .;c:\source; | c:\ | OK |
| c:\source\water | .; | c:\source | OK |
| c:\source\water | .;c:\; | c:\source | OK |
| c:\source\water | .;c:\source; | c:\source | OK |
| c:\source\water | .;any path; | c:\source | OK(主要是classpath设.的关系) |
| c:\source\java\water | .; | c:\ | package water does not exit; |
| c:\source\java\water | .; | c:\source | package water does not exit; |
| c:\source\java\water | .; | c:\source\java | OK |
| d:\source\java\water | .; | d:\source\java | package water does not exit; |
噫!”.”不是依照目前的目录去找package的class吗?可是放在d:时,好像就不是这样了,看来java的编译规则的classpath的”.”是指针对c:讲的,我终于有点恍然大悟,但是很多课本及网页都说是依照目前的目录去搜寻class的path,看来是有很大的争议的,不过有这个测试之后,感觉厘清了很多观念!