Java basics-knowledge about jar packages, Java jar packages

Source: Internet
Author: User

Java basics-knowledge about jar packages, Java jar packages

Before learning the jar package, you must first understand the Java package and related concepts about the Java package.

I. Package

Java provides a package mechanism to better organize classes. A package is a class container used to separate class namespace. If no package name is specified, all examples belong to a default untitled package.

Format:

Package pkg1 [. pkg2 [. pkg3…];

Code example:

1/** 2 * @ version 2017-06-17 3 * @ author zhou 4 */5 6 7 package cn.com. zhouzhou; // The package name must consist of 8 public class Lession1 {9 public static void main (String [] args) {10 System. out. println ("This is the first file I created under the package"); 11} 12}

Note:

1. If a package statement exists in the program, this statement must be the first executable statement in the source file;

2. Only comments or empty lines can be added before the package statement;

3. A file can contain only one package statement;

4. The package name has a hierarchical relationship, and each layer is separated by points (in the preceding example, "cn.com. zhouzhou ");

(Figure: Demo)

5. The package level must have the same structure as the file system of the Java Development System;

6. Generally, the package name must contain lowercase letters;

7. When using the package description, the program does not need to reference (import) the same package or any element of the package;

8. The import statement is only used to introduce classes in other packages into the current namespace. The current package is always in the current namespace.

 

* Example:

1) used during compilation: (my file already has an edisk)

E: \> javac-d. Lession1.java // you can generate the corresponding folder structure.

2) The full class name must be executed during execution.

Java cn.com. zhouzhou. Lession1

Note:

1. classes in the same source file are all in the same package. The content in the same package is accessed from each other without importing packages;

2. The following two sentences are provided during package import;

Import cn.com. zhouzhou. beans .*;

Import cn.com. zhouzhou. beans. UserInfo;

These two statements are only faster when the latter is compiled, and there is no difference during running.

3. If you want to set classpath, you need to set it to the directory of the outermost folder, for example:

Package cn.com. zhouzhou;

Name of the folder where set classpath = cn is located

4. There is no relation between the parent package and the Child package. As long as the parent package is not in the same package, the package must be imported when referenced;

5. In future development, if a class name cannot be found, the main reasons are as follows:

1) the class name is wrong.

2) The package name is incorrect.

3) The classpath settings are incorrect.

6. For classes under the java. lang package, you can use it without importing the package.

Ii. jar package

The jar file in java is installed with the. class file. It is a type of compression that is compatible with zip and is called a jar package. Many classes provided by JDK are also provided in the form of jar packages.

When using it, there are many classes in your file. compressing these classes and their directories into a file for others will become more professional and organized. After someone else obtains the jar package, as long as their classpath settings contain this jar file, the Java Virtual Machine will automatically decompress the jar file when loading the class, and treat it as a directory, and then find the structure of the package name and corresponding directory of the Class and Class we want in the directory.

 

How to Create a Jar package? The following describes the specific steps:

1. files that need to be packaged into a jar package

1 package cn.com. zhouzhou; // The package name must consist of 2 public class Lession2 {3 public static void main (String [] args) {4 System. out. println ("this is the file I want to package in jar"); 5} 6}

2. Compile

E: \> javac-d. Lession2.java

(There must be spaces on both sides of vertices after d)

3. Create a Jar package

Jar-cvf myjarbao. jar cn

(Generate a file named myjarbao. jar. Do not forget the following cn)

The result is as follows:

 

 

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.