您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Java 中如何使用package,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
1,聲明一個包,用package,放在文件的第一行,如:
package com.ghostwu.HelloWorld; public class HelloWorld{ public static void main( String[] args ){ System.out.println( "hello world" ); } }
當源代碼中聲明了一個包的時候,在編譯的時候,需要這樣做:【javac -d . HelloWorld.java】, -d: 后面指定編譯生成的class文件存放的目錄,
這里面的點(.)指的是當前目錄。
ghostwu@dev:~/java/data_struct/package$ ls com ghostwu.jar HelloWorld.java Student.java Test2.java Test.java ghostwu@dev:~/java/data_struct/package$ rm -rf com ghostwu.jar ghostwu@dev:~/java/data_struct/package$ ls HelloWorld.java Student.java Test2.java Test.java ghostwu@dev:~/java/data_struct/package$ javac -d . HelloWorld.java ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Student.java Test2.java Test.java ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu └── HelloWorld └── HelloWorld.class
我們也可以換個路徑,如:我把它編譯到~/tmp目錄下
ghostwu@dev:~/java/data_struct/package$ ls ~/tmp allpy.tar.gz ghostwu@dev:~/java/data_struct/package$ javac -d ~/tmp HelloWorld.java ghostwu@dev:~/java/data_struct/package$ ls ~/tmp allpy.tar.gz com ghostwu@dev:~/java/data_struct/package$ tree ~/tmp/com /home/ghostwu/tmp/com └── ghostwu └── HelloWorld └── HelloWorld.class 2 directories, 1 file
2,接下來,我們定義兩個類,來使用包
ghostwu@dev:~/java/data_struct/package$ cat Student.java package com.ghostwu.Student; public class Student { public void say(){ System.out.println( "my name is ghostwu" ); } }
ghostwu@dev:~/java/data_struct/package$ cat Test.java package com.ghostwu.Test; public class Test { public static void main( String[] args ){ com.ghostwu.Student.Student stu = new com.ghostwu.Student.Student(); stu.say(); } }
在Test類中,如果需要使用Student類,需要使用 包名.類名( com.ghostwu.Student.Student )
ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu └── HelloWorld └── HelloWorld.class 2 directories, 1 file ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Student.java Test2.java Test.java ghostwu@dev:~/java/data_struct/package$ javac -d . Student.java ghostwu@dev:~/java/data_struct/package$ javac Test.java ghostwu@dev:~/java/data_struct/package$ java Test Error: Could not find or load main class Test
在上面,編譯使用的過程中,我們發現,直接執行java test報了一個錯誤。這是因為我們Test也有聲明包( com.ghostwu.Test ),所以,我們應該這樣執行:
ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu ├── HelloWorld │ └── HelloWorld.class └── Student └── Student.class 3 directories, 2 files ghostwu@dev:~/java/data_struct/package$ javac -d . Test.java ghostwu@dev:~/java/data_struct/package$ java Test Error: Could not find or load main class Test ghostwu@dev:~/java/data_struct/package$ java com.ghostwu.Test.Test my name is ghostwu
執行格式:【java 包名+類名】
3,你可能已經發現了,這種方式非常麻煩,在使用包,每次實例化都要new xxx(包名) = xxx(包名) .... ,我們可以通過import關鍵字,引入包,就不需要每次加上包前綴了。
ghostwu@dev:~/java/data_struct/package$ cat Test2.java package com.ghostwu.Test2; import com.ghostwu.Student.Student; public class Test2 { public static void main( String[] args ){ Student stu = new Student(); stu.say(); } }
ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu ├── HelloWorld │ └── HelloWorld.class ├── Student │ └── Student.class └── Test └── Test.class 4 directories, 3 files ghostwu@dev:~/java/data_struct/package$ javac -d . Test2.java ghostwu@dev:~/java/data_struct/package$ java Test2 Error: Could not find or load main class Test2 ghostwu@dev:~/java/data_struct/package$ java com.ghostwu.Test2.Test2 my name is ghostwu
4,把所有的class打包成一個jar文件,稱之為jar包,比如,我們把當前目錄下的所有class文件,打成一個jar包,可以給別人使用了
【jar cvf ghostwu.jar com】 把 com目錄下的所有文件 打包成一個jar文件, c:創建jar文件 v:生產詳細信息 f:指定jar包的名稱,e 指定jar 包主程序入口
ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu ├── HelloWorld │ └── HelloWorld.class ├── Student │ └── Student.class ├── Test │ └── Test.class └── Test2 └── Test2.class 5 directories, 4 files ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Student.java Test2.java Test.class Test.java ghostwu@dev:~/java/data_struct/package$ jar cvf ghostwu.jar com added manifest adding: com/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/HelloWorld/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/HelloWorld/HelloWorld.class(in = 448) (out= 302)(deflated 32%) adding: com/ghostwu/Test2/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/Test2/Test2.class(in = 347) (out= 254)(deflated 26%) adding: com/ghostwu/Student/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/Student/Student.class(in = 420) (out= 293)(deflated 30%) adding: com/ghostwu/Test/(in = 0) (out= 0)(stored 0%) adding: com/ghostwu/Test/Test.class(in = 344) (out= 255)(deflated 25%) ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Test2.java Test.java ghostwu.jar Student.java Test.class
5,執行jar包的時候,發現報錯了,需要修改 MANIFEST.MF,指定入口
ghostwu@dev:~/java/data_struct/package$ java -jar ghostwu.jar no main manifest attribute, in ghostwu.jar 我們需要在ghostwu.jar包中,配置一個入口類
再次執行,就可以了
ghostwu@dev:~/java/data_struct/package$ java -jar ghostwu.jar my name is ghostwu
或者在生成jar包時就知道住程序入口,這樣更簡單,
jar -cvfe liujinjie.jar com/ghostwu/Test/Test com java -jar liujinjie.jar
執行jar包中指定main方法
1. 命令行指定 java -classpath ****.jar ****.****.className (java -classpath liujinjie.jar com.ghostwu.Test.Test)
2. maven方法
6,解壓jar包【jar -xvf ghostwu.jar 】
ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Test2.java Test.java ghostwu.jar Student.java Test.class ghostwu@dev:~/java/data_struct/package$ rm -rf com ghostwu@dev:~/java/data_struct/package$ ls ghostwu.jar HelloWorld.java Student.java Test2.java Test.class Test.java ghostwu@dev:~/java/data_struct/package$ jar -xvf ghostwu.jar created: META-INF/ inflated: META-INF/MANIFEST.MF created: com/ created: com/ghostwu/ created: com/ghostwu/HelloWorld/ inflated: com/ghostwu/HelloWorld/HelloWorld.class created: com/ghostwu/Test2/ inflated: com/ghostwu/Test2/Test2.class created: com/ghostwu/Student/ inflated: com/ghostwu/Student/Student.class created: com/ghostwu/Test/ inflated: com/ghostwu/Test/Test.class ghostwu@dev:~/java/data_struct/package$ ls com HelloWorld.java Student.java Test.class ghostwu.jar META-INF Test2.java Test.java ghostwu@dev:~/java/data_struct/package$ tree com com └── ghostwu ├── HelloWorld │ └── HelloWorld.class ├── Student │ └── Student.class ├── Test │ └── Test.class └── Test2 └── Test2.class 5 directories, 4 files
關于Java 中如何使用package就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。