Learn Java's keyst key

xiaoxiao2021-03-06  99

The first is the problem of pat and classpath

The second is Package and Import issues

The third is public, protected, private, static, what is used, why is it used, how to use

The fourth is to make JAR files

First, Path and ClassPath problems

What is path

Is your JDK work path

For example, your JDK is installed in C: / JDK / Your Path at this time (if there is no PATH) set to Path; C: / JDK / BIN

What is ClassPath

ClassPath is an important concept in Java, which describes the classes that the Java virtual machine is loaded in which paths to run in the path when running a Class and the class to run. Simply put, when a program does not find other class files he need, the system will automatically go to the path specified by the ClassPath environment variable to find the class and user-defined classes provided by third parties.

ClassPath and Java package relationship

Java's package is closely related to the ClassPath relationship. The package is "." Segmentation, Sun recommends using the retrograde of the domain name to distinguish between different packages to avoid conflicts, such as com.company.util. In a package, you need to store the same directory as the package name when stored, such as sample.class in the com.company.util package, to be stored in the COM / COMPANY / UTIL directory.

ClassPath has two expressions, one is a classpath, such as C: / Work / Classes, indicating that the C: / Work / Classes directory is a ClassPath entry; another way is ClassPath, such as C: /work/util.jar, indicating that the C: /Work/util.jar file is a ClassPath entry, and any compressed file containing the Java class can be used as an entry of ClassPath.

So what is the relationship between ClassPath and the package? Simply put, Java virtual machines look for specific class files in such a way while loading classes: ClassPath packets stored directory specific class files. If there is a C: / Work / Classes entry in ClassPath, the class that needs to be loaded is com.company.util.sample.class, then when loading this class, the virtual opportunity looks up C: / Work / Classes / COM / Company / Util directory, if sample.class is in this directory, the virtual machine can be found, if this class is not in this directory, not in any other classpath, then the virtual opportunity throws a ClassNotFoundException.

ClassPath's order and class conflict

The Java virtual machine finds the classpath when loading the class, if there are multiple entries in the classpath, there are classes of the same name, then the class in the previous position will be loaded, and the later will be ignored. This type of sub-loading in order may result in a class conflict. For example, classpath = c: /servlet2.2/servlet.jar; c: /servlet2.3/servlet.jar, then in the process of practical applications, you use servlet2.2, not servlet2.3. Many times if you don't pay attention to this, it may result in strange anomalies.

ClassPath settings in the command line status

ClassPath in the command line can be set in two ways.

One is to set the environment variable directly, for example in the Windows environment, we use the set command: set classpath = c: / work / classes; C: /Work/util.jar

Another way is to specify ClassPath directly when executing Javac, Java or other Java commands:

Java-ClassPath C: / Work / Classes; C: /Work/util.jar com.company.util.sample

two

Package and Import Problem

Package in Java is actually a directory, which is to better manage the Java class (Class) and Interface. The java language package can be used by another Java development package. If we want to reference the classes in a package, use the import keyword to indicate that the package will be in the path to the ClassPath before the Package is packed before the Package is packaged.

For example, ClassPath is C: / Test

Package com.dir

The result is a COM subdirectory will be generated in the C: / Test directory. The directory CoM contains all files of DIR directory will be placed on the DIR directory.

In the embodiment of the object in Java, in the definition and application of the package, for the already edited class, if you want to use in the new class, you should contain the already defined class, and the IMPORT statement is implemented in Java.

Use the Import statement to join all classes (with above Package packages as an example)

IMPORT COM.DIR. *;

Use the import statement of the import statement (with the above package package as an example, if there is Hello.Class in the directory)

Import com.dir.hello;

Third, public, protected, private, static, what is used, why is it used, how to use

Each class has created its own namespace, and the method and variables can know each other's existence, which can be used.

Public classes can not only be used by other classes in the same package, but the classes in other packages can also be used;

Variables and methods for modifying the word public, protected, private:

Public: Any other classes, the object can only access the data of the variable, or how the method can be used.

Class ABC {

Public int pub_i = 5;

Public void show) () {

System.out.println ("pub_i" pub_i);

}

Class demo {

Public static void main (string args []) {

ABC ABC = New ABC ();

System.out.println ("abc.pub_i" abc.pub_i);

ABC.PUB_I = 10;

ABC.SHOW ();

}

}

protected variables and methods:

If there is a variable or method in a class, there is a modified word protected, the same class, the same package can be used. The class of different packages should be used, and must be a subclass of such a class, and a variable or call can be accessed.

PUBLIC CLASS ABC {

protected int pro_i = 5;

protected void show () {

System.out.println ("Pro_i =" Pro_i);

}

Class:

Class def {

Public static void main (string args []) {

ABC ABC = New ABC ();

System.out.println ("abc.pro_i =" abc.pro_i);

ABC.PUB_I = 10;

ABC.SHOW ();

}

}

Different packages but are subclasses:

Import mytest.pack.abc;

Class Def Extends ABC {

Public static void main (string agrs []) {

DEF DEF = New DEF ();

System.out.println (Def.i);

DEF.I = 10;

DEF.SHOW ();

}

Private does not allow any other class access and call;

The variable name in the subclass is the same as the parent class, and the original variable is covered.

Overriding and overloading. The method defined in the children and grandchildren is the same name, the same parameter line, the method in the ancestral class is covered; the overloader of the method refers to the polymorphism of an object, that is, multiple methods with the same name But the parameter line is different.

Final:

Final prevented the method from being covered before the method;

Final Before class, the standard is that this class cannot be inherited;

Final defines a constant before the variable.

Static:

Before variables or methods, they are derived;

The static variable is shared between the examples. If it is a PUBLIC static variable, other classes may not be accessed by instantiation;

Static method is called a class method, so it is called without instantiation (the process)

A object's approach can access the object's data member, although it is not partial variable that is not a method; a class method can only access its local variable.

Example: incorrect reference

Class staticError {

String mystring = "hello";

Public static void main (string args []) {

System.out.println (MyString);

}

Error message: can't make a static reference to nonstatic variable.

Why isn't it correct? Only the object's method can access the variable of the object.

The solution:

1) Remove variables to class variables

Class staticError {

Static string mystring = "hello";

Public static void main (string args []) {

System.out.println (MyString);

}

2) Create an instance of a class

Class NostaticError {

Public static void main (string args []) {

String mystring = "hello";

System.out.println (MyString);

}

}

Fourth, make JAR files

The JAR file is Java Archive File, and the appraisal, its application is closely related to Java, a document format of Java. The JAR file is very similar to zip file - accurately, it is a zip file, so it is called a file package. The only difference between the JAR file and the ZIP file is to include a meta-inf / manifest.mf file in the content of the JAR file. This file is automatically created when generating a JAR file.

JAR command details

JAR is installed with JDK, in the bin directory in the JDK installation directory, the file name under Windows is jar.exe, and the file name under Linux is JAR. It runs to use the Tools.jar file in the Lib directory in the JDK installation directory. However, in addition to installing JDK, we don't need to do anything, because Sun has helped us do well. We don't even need to put Tools.jar in the ClassPath. Use no jar commands that we can see that the JAR command is as follows:

Jar {ctxu} [vfm0m] [jar- file] [manifest- file] [-c directory] file name ...

Where {ctxu} is the subcommand of the jar command, each JAR command can only contain one of the CTXU, which represent:

-c Create a new JAR file package

-T lists the content list of JAR file packages

-x Expand the specified file or all files of the JAR file package

-u Update existing JAR file package (add file to the JAR file)

The options in [VFM0M] can be optionally or not, they are the option parameters of the jar command.

-v generation detailed report and prints to standard output

-f Specifies the JAR file name, usually this parameter is necessary

-m Specifies the Manifest manifest file that needs to be included.

-0 only stores, does not compress, which generated JAR file package is larger than the volume of the parameter, but the speed is faster

-M does not generate a list of all items (MANIFEST) file, this parameter ignores -m parameters

[JAR-File] The JAR file package needs to be generated, viewed, updated, or unlocked, which is the accessory parameter of the -f parameter

[Manifest- File] MANIFEST list file, it is an accessory parameter of -m parameter

[-C Directory] Indicates to the operation of the specified directory to execute this JAR command. It is equivalent to using the CD command to execute the JAR command without -c parameters in the directory, which can only be available when creating and updating the JAR file package.

File name ... Specify a file / directory list, which is to add the file / directory to the JAR file. If the directory is specified, the JAR command will automatically put all the files and subdirectories in the directory into the package.

Let's give some examples to illustrate the usage of JAR commands:

1) JAR CF TEST.JAR TEST

This command does not have the display of the execution process, the execution result is the Test.jar file in the current directory. If the current directory already has Test.jar, then the file will be overwritten.

2) JAR CVF TEST.JAR TEST

This command is the same as the result in the previous example, but due to the function of V parameters, the package process is displayed, as follows:

Mandarin list (Manifest)

Add: TEST / (read = 0) (write = 0) (0% stored)

Add: Test / Test.class (read = 7) (write = 6) (40% compressed)

3) JAR CVFM TEST.JAR TEST

This command is similar to 2), but does not contain meta-inf / manifest files in the generated Test.jar, the information of the package process is slightly different:

Add: TEST / (read = 0) (write = 0) (0% stored)

Add: Test / Test.class (read = 7) (write = 6) (40% compressed)

4) JAR CVFM Test.jar Manifest.mf Test

Running results and 2) Similar, display information is the same, just generate the meta-inf / manifest content in the JAR package, which contains Manifest.mf. 5) Jar Tf Test.jar

In the case where Test.jar already exists, you can view the contents of Test.jar, such as 2) and 3), should be this command, and the results are as follows;

For 2)

META-INF /

META-INF / Manifest.mf

TEST /

TEST / TEST.CLASS

For 3)

TEST /

TEST / TEST.CLASS

6) Jar TVf Test.jar

In addition to displaying the content shown in 5), it also includes detailed information in the package file, such as:

0 Wed Jun 19 15:39:06 GMT 2002 Meta-Inf /

86 WED JUN 19 15:39:06 GMT 2002 Meta-Inf / Manifest.mf

0 WED JUN 19 15:33:04 GMT 2002 TEST /

7 Wed Jun 19 15:33:04 GMT 2002 TEST / TEST.CLASS

7) JAR Xf Test.jar

Unlock Test.jar to the current directory, do not display any information, for 2) The resulting Test.jar, the unlocked directory structure is as follows:

==

| - META-INF

| `- manifest

`- test

`- Test.class

8) JAR XVF Test.jar

The operation results are the same as 7), and there is more information for the decompression process, such as:

Create: Meta-Inf /

Expand: meta-inf / manifest.mf

Create: TEST /

Expand: Test / Test.class

9) jar uf test.jar manifest.mf

Add file manifest.mf in Test.jar, this use JAR TF to see Test.jar can find that Test.jar is more than one manifest than the original. By the way, if you use the -m parameter and specify the manifest.mf file, then manifest.mf is used as a manifest file manifest, and its content will be added to Manifest; however, if it is added as a general file to the JAR file In the package, it is not different from general documents.

10) Jar UVF Test.jar Manifest.mf

The same is the same as 9), and there is a detailed information, such as:

Add: manifest.mf (read = 17) (write = 19) (compressed -11%)

Some skills about JAR file packages

1) Use unzip to extract the JAR file

When introducing the JAR file, the JAR file is actually a zip file, so you can use a common tool to extract the Zip file to extract the JAR file, such as WinD, WinRar et al. And Linux under WinDOP, etc. . Using Winzip and WinRAR, the decompression is because they decompressed more intuitive and convenient. Use unzip because it can specify the target directory using the -d parameter when it decompresses.

When decompressing a JAR file, it is not possible to use the JAR-C parameter to specify the decompressed target, because the -c parameter is only available when creating or updating the package. Then you need to extract the file to a specified directory, you need to copy this JAR file to the target directory, and then decompressed, trouble. If you use unzip, you don't need to be so troublesome, just you need to specify a -D parameter. Such as: unzip test.jar -d DEST /

2) Creating a JAR file using tools such as Winzip or WinRAR

The JAR file mentioned above is a ZIP file containing meta-inf / manifest, so you only need to use Winzip, WinRAR and other tools to create the ZIP compressed package, and add a Meta-inflight containing the manifest file. Catalog. For the case of using the -m parameter using the jar command, only the manifest is required to modify this manifest.

3) Create a ZIP file using the jar command

Some Linux provide UNZIP commands, but there is no zip command, so you need to decompress the ZIP file, you can't create a zip file. To create a zip file, use the JAR command with the -m parameter, because the -m parameter indicates that the manifest list is not added when making the JAR package, then only needs to change the .jar extension in the specified target JAR file. The .zip extension, created is a unclatible ZIP file, such as the third) example of the previous section is slightly changed:

Jar Cvfm Test.zip Test

转载请注明原文地址:https://www.9cbs.com/read-124405.html

New Post(0)