千家信息网

Java 中如何使用package

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章将为大家详细讲解有关Java 中如何使用package,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1,声明一个包,用package,放在文件
千家信息网最后更新 2025年02月03日Java 中如何使用package

这篇文章将为大家详细讲解有关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$ lscom  ghostwu.jar  HelloWorld.java  Student.java  Test2.java  Test.javaghostwu@dev:~/java/data_struct/package$ rm -rf com ghostwu.jarghostwu@dev:~/java/data_struct/package$ lsHelloWorld.java  Student.java  Test2.java  Test.javaghostwu@dev:~/java/data_struct/package$ javac -d . HelloWorld.java ghostwu@dev:~/java/data_struct/package$ lscom  HelloWorld.java  Student.java  Test2.java  Test.javaghostwu@dev:~/java/data_struct/package$ tree comcom└── ghostwu    └── HelloWorld        └── HelloWorld.class

我们也可以换个路径,如:我把它编译到~/tmp目录下

ghostwu@dev:~/java/data_struct/package$ ls ~/tmpallpy.tar.gzghostwu@dev:~/java/data_struct/package$ javac -d ~/tmp HelloWorld.java ghostwu@dev:~/java/data_struct/package$ ls ~/tmpallpy.tar.gz  comghostwu@dev:~/java/data_struct/package$ tree ~/tmp/com/home/ghostwu/tmp/com└── ghostwu    └── HelloWorld        └── HelloWorld.class2 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 comcom└── ghostwu    └── HelloWorld        └── HelloWorld.class2 directories, 1 fileghostwu@dev:~/java/data_struct/package$ lscom  HelloWorld.java  Student.java  Test2.java  Test.javaghostwu@dev:~/java/data_struct/package$ javac -d . Student.java ghostwu@dev:~/java/data_struct/package$ javac Test.javaghostwu@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 comcom└── ghostwu    ├── HelloWorld    │   └── HelloWorld.class    └── Student        └── Student.class3 directories, 2 filesghostwu@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 Testghostwu@dev:~/java/data_struct/package$ java com.ghostwu.Test.Testmy 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 comcom└── ghostwu    ├── HelloWorld    │   └── HelloWorld.class    ├── Student    │   └── Student.class    └── Test        └── Test.class4 directories, 3 filesghostwu@dev:~/java/data_struct/package$ javac -d . Test2.java ghostwu@dev:~/java/data_struct/package$ java Test2Error: Could not find or load main class Test2ghostwu@dev:~/java/data_struct/package$ java com.ghostwu.Test2.Test2my 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 comcom└── ghostwu    ├── HelloWorld    │   └── HelloWorld.class    ├── Student    │   └── Student.class    ├── Test    │   └── Test.class    └── Test2        └── Test2.class5 directories, 4 filesghostwu@dev:~/java/data_struct/package$ lscom  HelloWorld.java  Student.java  Test2.java  Test.class  Test.javaghostwu@dev:~/java/data_struct/package$ jar cvf ghostwu.jar comadded manifestadding: 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$ lscom          HelloWorld.java  Test2.java  Test.javaghostwu.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 comjava -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$ lscom          HelloWorld.java  Test2.java  Test.javaghostwu.jar  Student.java     Test.classghostwu@dev:~/java/data_struct/package$ rm -rf comghostwu@dev:~/java/data_struct/package$ lsghostwu.jar  HelloWorld.java  Student.java  Test2.java  Test.class  Test.javaghostwu@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.classghostwu@dev:~/java/data_struct/package$ lscom          HelloWorld.java  Student.java  Test.classghostwu.jar  META-INF         Test2.java    Test.javaghostwu@dev:~/java/data_struct/package$ tree comcom└── ghostwu    ├── HelloWorld    │   └── HelloWorld.class    ├── Student    │   └── Student.class    ├── Test    │   └── Test.class    └── Test2        └── Test2.class5 directories, 4 files

关于Java 中如何使用package就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0