1. 程式人生 > >java package(包)的用法

java package(包)的用法

ghost fin clas 編譯 pub 可能 ati 打包成 .com

一般來說都用eclipse自動化圖形工具搞定,我用的是ubuntu,所以需要自己打包引入。

什麽是包?

這是對java源代碼的組織和管理的一種方式,比如:當操作系統某個目錄的文件非常多的時候,我們一般建立子目錄分類管理,java中的包跟文件目錄的作用類似。

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 2 directories, 1 file

我們也可以換個路徑,如:我把它編譯到~/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包的名稱

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包的時候,發現報錯了

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

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(包)的用法