Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use package in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use package in Java. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

1. Declare a package, using package, on the first line of the file, such as:

Package com.ghostwu.HelloWorld;public class HelloWorld {public static void main (String [] args) {System.out.println ("hello world");}}

When a package is declared in the source code, you need to do this at compile time: [javac-d. HelloWorld.java],-d: specify the directory where the compiled class files are stored

The point in this (.) Refers to the current directory.

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

We can also change the path, for example, I compiled it to the ~ / tmp directory

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, next, let's define two classes to use the package

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 ();}}

In the Test class, if you need to use the Student class, you need to use the package name. Class name (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

In the process of compiling above, we found that the direct execution of java test reported an error. This is because we Test also have a declaration package (com.ghostwu.Test), so we should do this:

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

Execution format: [java package name + class name]

3, you may have found that this method is very troublesome. When using a package, new xxx (package name) = xxx (package name) is required for each instantiation. We can introduce the package through the import keyword so that we don't need to prefix the package every time.

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. Package all the class into a jar file, which is called the jar package. For example, we pack all the class files in the current directory into a jar package, which can be used by others.

[jar cvf ghostwu.jar com] package all the files under the com directory into a jar file, c: create the jar file v: production details f: specify the name of the jar package, e specify the main program entry of the jar package

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) 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. When executing the jar package, an error is found. You need to modify the MANIFEST.MF and specify the entry.

Ghostwu@dev:~/java/data_struct/package$ java-jar ghostwu.jar no main manifest attribute, in ghostwu.jar We need to configure a portal class in the ghostwu.jar package

Just execute it again.

Ghostwu@dev:~/java/data_struct/package$ java-jar ghostwu.jar my name is ghostwu

Or know to live at the program entrance when generating the jar package, which is easier.

Jar-cvfe liujinjie.jar com/ghostwu/Test/Test comjava-jar liujinjie.jar

Execute the main method specified in the jar package

1. The command line specifies java-classpath *. Jar *. ClassName (java-classpath liujinjie.jar com.ghostwu.Test.Test)

2. Maven method

6. Extract the jar package [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, so much for sharing about how to use package in Java. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report