首頁 / CompScience / Programming / Languages / java / 玩玩java classpath package import

玩玩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:\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,看來是有很大的爭議的,不過有這個測試之後,感覺釐清了很多觀念!

Print Friendly, PDF & Email
馬上成為工作達人的Fans

About ㄚ琪

工作達人Fun Taiwan的創辦者及總編,可以在這裡更認識他。

發表迴響

你的電子郵件位址並不會被公開。 Required fields are marked *

*

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

Scroll To Top