Usage of the Java package (packages)

Source: Internet
Author: User

In general, I use the Eclipse Automation Graphics tool, I use Ubuntu, so I need to package the introduction.

What is a package?

This is a way of organizing and managing Java source code, such as: when there are very many files in a directory of the operating system, we generally create sub-directory classification management, Java package and file directory function similar.

1, declare a package, and put it in the first line of the file, such as:

 Package Com.ghostwu.HelloWorld;  Public class helloworld{    publicstaticvoid  main (string[] args) {        "Hello World " );}    }

When a package is declared in the source code, it needs to be done at compile time: "javac-d. Helloworld.java ",-D: After specifying the directory where the generated class file is compiled,

Here's The dot (.) Refers to the current directory.

[Email protected]:~/java/data_struct/ Package$ lscom Ghostwu.jar helloworld.java Student.java test2.java test.java[email protected]:~/java/data_struct/ Package$ RM-RF com ghostwu.jar[email protected]:~/java/data_struct/ Package$ lshelloworld.java Student.java test2.java test.java[email protected]:~/java/data_struct/ Package$ javac-d. Helloworld.java [email protected]:~/java/data_struct/ Package$ lscom Helloworld.java student.java Test2.java test.java[email protected]:~/java/data_struct/ Package$ tree Comcom└──ghostwu└──helloworld└──helloworld.class2 directories, 1 file

We can also change the path, such as: I compiled it into the ~/tmp directory

[Email protected]:~/java/data_struct/Package $ ls ~/tmpallpy.tar.gz[email protected]:~/java/data_ struct/Package $ javac-d ~/tmp Helloworld.java [email protected]:~/java/data_struct/Package $ ls ~/tmpallpy.tar.gz  com[email protected]:~/java/data_struct/Package $ tree ~/tmp/  COM/home/ghostwu/tmp/com└──ghostwu    └──helloworld        └──helloworld. class2 directories, 1 file

2, Next, we define two classes to use the package

 [Email protected]:~/java/data_struct/package  $ Cat Student.java  package   com.ghostwu.Student;  public  class   Student { void   Say () {System.out.println (" My name is Ghostwu " ); }}
[Email protected]:~/java/data_struct/ package com.ghostwu.Test;  Public class Test {    publicstaticvoid  main (string[] args) {        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)

 [Email protected]:~/java/data_struct/package  $ tree Comcom└──ghostwu└──helloworld└──helloworld.  class  2 directories, 1 file[email  Protected]:  ~/java/data_struct/package  $ lscom Helloworld.java Student.java Test2.java test.java[email protected]:  ~/java/data_struct/package  $ javac-d. Student.java [email protected]:  ~/java/data_struct/package  $ Javac test.java[email protected]:  ~/java/data_struct/package  Span style= "color: #000000;" >$ Java Test error:could not find or load main  class  Test 

In the process of compiling using, 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:

 [Email protected]:~/java/data_struct/package  $ tree Comcom└──ghostwu├──helloworld│  └──helloworld.  class  └──student└──student.  class  3 directories, 2 files[email  Protected]:  ~/java/data_struct/package  $ javac- d. Test.java [email protected]:  ~/java/data_struct/package  $ Java Test error:could not find or load main  class   test[email protected]:  ~/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 way is very troublesome, in the use of the package, each instantiation of the new XXX (package name) = XXX (package name) ...., we can introduce the package through the Import keyword, we don't need to add the package prefix each time.

[Email protected]:~/java/data_struct/ package com.ghostwu.Test2; Import com.ghostwu.Student.Student;  Public class Test2 {    publicstaticvoid  main (string[] args) {          New  Student ();        Stu.say ();    }}
[Email protected]:~/java/data_struct/ Package$ tree Comcom└──ghostwu├──helloworld│└──helloworld.class├──student│└──student.class└──test└──test.class4 Directories, 3Files[email protected]:~/java/data_struct/ Package$ javac-d. Test2.java [email protected]:~/java/data_struct/ Package$ Java test2error:could not find or load mainclassTest2[email protected]:~/java/data_struct/ Package$ java com.ghostwu.Test2.Test2my name is GHOSTWU

4, packaging all the classes into a jar file, called the jar package, for example, we put all the class files in the current directory into a jar package, can be used by others

"Jar CVF ghostwu.jar com" Packages all files under the COM directory into a jar file, C: Create Jar file V: Production details F: Specify the name of the jar package

[Email protected]:~/java/data_struct/ Package$ tree Comcom└──ghostwu├──helloworld│└──helloworld.class├──student│└──student.class├──test│└──test.class└──test2└──test2.class5 Directories, 4Files[email protected]:~/java/data_struct/ Package$ lscom Helloworld.java student.java Test2.java Test.classTest.java[email protected]:~/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%) [email protected]:~/java/data_struct/ Package$ lscom Helloworld.java test2.java Test.javaghostwu.jar student.java Test.class

5, when executing the jar package, found an error

[Email protected]:~/java/data_struct/Package $ java-jar Ghostwu.jar No main manifest attribute, in Ghostwu.jar

We need to configure an Ingress class in the Ghostwu.jar package.

Run it again, you're done.

[Email protected]:~/java/data_struct/Package $ java-jar Ghostwu.jar My name is GHOSTWU

6. Unpack the jar package "JAR-XVF Ghostwu.jar"

[Email protected]:~/java/data_struct/ Package$ lscom Helloworld.java test2.java Test.javaghostwu.jar student.java Test.class[email protected]:~/java/data_struct/ Package$ RM-RF Com[email protected]:~/java/data_struct/ Package$ lsghostwu.jar Helloworld.java student.java Test2.java Test.classTest.java[email protected]:~/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.classcreated:com/ghostwu/test2/inflated:com/ghostwu/test2/test2.classcreated:com/ghostwu/student/inflated:com/ghostwu/student/student.classcreated:com/ghostwu/test/inflated:com/ghostwu/test/test.class[email protected]:~/java/data_struct/ Package$ lscom Helloworld.java student.java Test.classGhostwu.jar META-INF Test2.java test.java[email protected]:~/java/data_struct/ Package$ tree Comcom└──ghostwu├──helloworld│└──helloworld.class├──student│└──student.class├──test│└──test.class└──test2└──test2.class5 directories, 4 files

Usage of the Java package (packages)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.