Home >Java >javaTutorial >Java basics - knowledge about jar packages

Java basics - knowledge about jar packages

巴扎黑
巴扎黑Original
2017-06-26 11:34:371265browse

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

1. Package

In order to better organize classes, Java provides a package mechanism. Packages are containers of classes that separate class namespaces. If no package name is specified, all examples belong to a default unnamed package.

The format is:

package pkg1[. pkg2[. pkg3…]];

Code example:

 1 /** 2 *@version 2017-06-17 3 *@author zhou 4 */ 5  6  7 package cn.com.zhouzhou;//包名一定要由小写字母组成 8 public class Lession1 { 9     public static void main(String[] args) {10         System.out.println("这是我创建的第一个在包下的文件");11         }12         }

Notes:

1. If there is a package statement in the program, the statement must be the source The first executable statement in the file;

2. There can only be comments or blank lines before the package statement;

3. There can be at most one package statement in a file;

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

(Picture: Demonstration)

5. The package hierarchy must be the same as the file system structure of the Java development system;

6. Usually all lowercase letters are used in the package name;

7 .When using the package specification, there is no need to reference (import) the same package or any element of the package in the program;

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 demonstration:

1) When compiling, use: (My file is stored on the E disk)

e:\>javac -d . Lession1.java // The corresponding folder structure can be generated

2) When executing, the full class name must be executed

java cn .com.zhouzhou.Lession1

Notes:

1. Classes in the same source file are all in the same package. Contents in the same package can access each other without import. Package;

2. When importing the package, follow the following two sentences;

import cn.com.zhouzhou.beans.*;

import cn.com.zhouzhou.beans. UserInfo;

These two sentences, the latter is slightly faster only when compiling, but there is no difference when running

3. If you want to set the classpath, set it to the outermost layer The directory where the folder is located, for example:

package cn.com.zhouzhou;

set classpath=the folder name where cn is located

4. For the parent package It has nothing to do with the use of sub-packages. As long as it is not in a package, the package must be imported when referencing it;

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

1) The class name is wrong

2) The package name is wrong

3) The classpath is set wrong

6. For java. Classes under the lang package can be used without importing the package.

2. Jar package

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

When you use it, there are many classes in your file. Compressing these classes and their directories into one file for others will look more professional and organized. After someone else gets this jar package, as long as his classpath settings include this jar file, the Java virtual machine will automatically decompress the jar file when loading the class, treat it as a directory, and then search for it in the directory. The class we want, the package name of the class and the structure of the corresponding directory.

So how to create a Jar package? Below I will demonstrate the specific steps:

1. Files that need to be packaged into jar packages

1 package cn.com.zhouzhou;//包名一定要由小写字母组成2 public class Lession2{3     public static void main(String[] args) {4         System.out.println("这是我要打jar包的文件");5         }6         }

2. Compile

e:\> javac -d . Lession2.java

(The dot after d must have spaces on both sides of the dot)

3. Create Jar package

jar -cvf myjarbao.jar cn

(Generate a file named myjarbao.jar; don’t forget the following cn)

The generated results are as follows:

The above is the detailed content of Java basics - knowledge about jar packages. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn