玩玩java classpath package import

這幾天碰到了編譯失敗的嚴重問題,所以只好痛下心來好好研究看看:

我的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:waterPetStore3.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:waterPetStore3.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:sourcewater.;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:sourcewater.;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:sourcewater.;c:source;c:OK
c:sourcewater.;c:sourceOK
c:sourcewater.;c:;c:sourceOK
c:sourcewater.;c:source;c:sourceOK
c:sourcewater.;any path;c:sourceOK(主要是classpath設.的關係)
c:sourcejavawater.;c:package water does not exit;
c:sourcejavawater.;c:sourcepackage water does not exit;
c:sourcejavawater.;c:sourcejavaOK
d:sourcejavawater.;d:sourcejavapackage water does not exit;

噫!”.”不是依照目前的目錄去找package的class嗎?可是放在d:時,好像就不是這樣了,看來java的編譯規則的classpath的”.”是指針對c:講的,我終於有點恍然大悟,但是很多課本及網頁都說是依照目前的目錄去搜尋class的path,看來是有很大的爭議的,不過有這個測試之後,感覺釐清了很多觀念!

感謝你看到這裡,很快就可以離開了,但最好的獎勵行動就是按一下幫我分享或留言,感恩喔~

點我分享到Facebook

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。