The first is the problem of Path 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 during 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 indicated by the ClassPath environment variable to find the relationship between the class and user-defined classes provided by third parties, ClassPath and Java packages. 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 version conflict Java virtual machine When you load classpath is order, if there are multiple entries in ClassPath, there are classs of the same name, then the class in the front position will be loaded, followed It 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. The classpath set in the command line state setting command line can be set in two ways.
One is to set the environment variables directly, for example in a Windows environment, we use the set command: set classpath = c: / work / classes; c: /Work/util.jar Another way to perform Javaac, Java or other Java Specify ClassPath: java -classpath c: / work / classes; c: /work/util.jar com.company.util.sample 2 package and import problem Java package (package) in java is the directory, it It is to better manage the Java class (Class) and interfaces. The java language package can be used by another Java development package. If we want to reference a classes in a package, use the import keyword to indicate that Package Bow before the IMPORT row, the package will generate the path to ClassPath, which is specified, for example, ClassPath, C: / TestPackage Com.dir results To generate a COM subdirectory in the C: / Test directory, the directory COM contains all files all files will be placed in the DIR directory in Java's objects in the definition and application of the package, for the already edited class, if In the new class, you should include the already defined class, and the IMPORT statement is implemented in Java. Use the Import statement to join all classes (with above Package package as an example) Import com.dir. *; Join the import statement Classic (with the above package package as an example, if there is hello.class) import com.dir.hello; third, public, protected, private, static, when used, why is it used, how to use each class Created your own namespace, the method and variables can know each other's existence, can be used. Public classes can not only be used by other classes in the same package, can also use classes in other packages; variables and methods for modifying the word public, protected, private: public: any other class, the object can see this class If it can 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 variable and method: If a variable or method has a modified protected, In 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);}}} package: 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 packets 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; as the variable name in the subclass is the same as the parent class, 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 before the method, prevents the method from being covered; factory before the class, the standard is that the class cannot be inherited; Final defines a constant before the variable. Static: Before the variable or method, it indicates that they belong to the class; the static variable is shared between the examples, if it is a PUBLIC static variable, other classes may not access them by instantiation; the static method is called a class method, so it is not Method for calling (oriented) to access an 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.
Solution: 1) Remove variables to change the variable Class StaticError {static string mystring = "hello"; public static void main (string args []) {system.out.println (mystring);}} 2) Creating a class first Example Class NostaticError {public static void main (string args []) {string mystring = "hello"; system.out.println (mYString);}}} 440
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 explain 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 the contents of 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