java package(包)的用法

通常來講都用eclipse自動化圖形工具搞定,我用的是ubuntu,因此須要本身打包引入。java

什麼是包?ubuntu

這是對java源代碼的組織和管理的一種方式,好比:當操做系統某個目錄的文件很是多的時候,咱們通常創建子目錄分類管理,java中的包跟文件目錄的做用相似。eclipse

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文件存放的目錄,spa

這裏面的點(.)指的是當前目錄。操作系統

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目錄下code

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,接下來,咱們定義兩個類,來使用包blog

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 )ip

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
相關文章
相關標籤/搜索