java package(包)的用法

一般来说都用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

 

posted @ 2018-07-07 12:42 ghostwu 阅读(...) 评论(...) 编辑 收藏
Copyright ©2017 ghostwu