Fully master the "package" mechanism in Java

zhaozj2021-02-16  124

text

The "package" mechanism is in Java, and is also the most basic knowledge in Java. Some friends of Java beginner, usually in other languages, just like some programs on the textbook, but often encounter inexplicable error tips. These issues are in fact, they are not clear enough to "package". This article will explain in depth on this issue.

First, why do you have a "package" in java?

In a priority, the main reason for the introduction of "package" in Java is the need for Java itself cross-platform characteristics. Because all resources in Java are also organized in a file manner, this mainly contains a lot of class files that require organization management. The directory tree structure is also used in Java. Although various common operating system platforms are organized in the form of a directory tree, they are different from the separation of directory, in order to distinguish in various platforms, "." Is used in Java to separate the directory.

Second, Java China Package Structure and Platform

The resources in Java will inevitably have a big difference when they are under different platforms. Therefore, a cross-platform Java package structure and platform must be connected together. In fact, they are connected together through our very familiar classpath. for example:

I set the classpath in the Windows2000 environment as follows:

ClassPath = D: /jdk1.4.2/lib/dt.jar; D: / CJM

The connection between classes can be expressed by the following figure:

As can be seen from the figure, the organization of the class in Java is "hanging", so that they can be placed in any platform, but to find a class in this platform, you must use the classpath to set the class. The front part of the directory (ie, the portion of the platform). In Java, a class tree is often shrunk into a .jar file, Rt.jar in the figure, does not affect the search of the class, can specify when specifying the environment variable, can specify the .jar file, or specify .jar's full search path, that is, in the classpath in the upper example, it can also be described:

ClassPath = D: /JDK1.4.2/lib; d: / cjm

When the ClassPath environment variable under the platform is set correctly, the characteristics of the Java cross-platform are reflected. That is, when you write a program, you don't have to specify its full path, but just indicate that the classpath in Java can, indicating that the lookup path on the right side of the vertical line in Figure 1 is OK. In this case, when you have written the program, you only need to write the corresponding ClassPath environment variable according to the storage directory of the class file without modifying the program because the storage environment changes.

Note: Java's lookup for a class is to connect each item in the classpath. When a connection can find the relevant class correctly, it will not look backward.

Third, use "package" correctly

There are a lot of small details that need attention during the use of the package, and the common problems are listed below:

1. There are usually two ways to set the class path:

i) Set in the environmental variable of the system, the setting method changes according to the platform;

II) Set in the form of command parameters.

Such as: javac -classpath d: /jdk1.4.2/lib d: /cjm/edu/test/testfile.java java -classpath.; D: /jdk1.4.2/lib; d: / cjm edu.test.testfile

Note: i) The Javac and Java commands have great differences. You can distinguish between this, Javac is a platform command, which operates for specific platform files, to specify the compiled file path. Java is a virtual machine command, which is a description of the class, that is, the description of the class, and cannot add the extension, pay attention to the case of the class name. II) There is a very strange question, that is, the ClassPath behind the javac command contains the current directory (conforming to Windows habits), but the classpath behind the java command does not contain the current directory, so it will not forget the plus in its classpath. The description of the current directory, add ".".

2. Use "." To be "." In the Java program, and also have the concept of the current directory. To run the testfile in Figure 1 must be specified as edu.test.testfile. But if you want to call and it in the class testfile, you don't have to specify the directory prefix.

3. All the categories used in the Java program should clearly indicate the search path of this class. There are generally two ways to indicate:

i) Indicate the import keyword in the beginning of the program. If you want to use the FileInputStream class in the class Testfile, add import java.io.fileReader in the program head; or import java.io. *;

II) Write the full path directly at the FileFileReader class, such as: java.io.fileReader fin = new java.io.fileReader ("FileName");

Note: Java.LANG package is always imported by default.

4, the catalog structure of the class must be consistent with the first "package declaration" in the class. If the class testfile.class corresponds to the first sentence of the Java file, you must include: package edu.test;

Make sure that the class's storage path and "Package path" specified in the class generally have two:

i) The directory stored when writing .java files, is determined in advance, such as TestFile.java directly in the EDU / TEST directory, then compile with the following statement:

Javac-ClassPath D: /jdk1.4.2/lib d: /cjm/edu/test/testfile.java

When the compilation is complete, the descriptive TestFile.class file will appear in the description path of the Java file in the compiling command. That is, in D: / Test / EDU / TEST

II) Compile the program through the use of the -d parameter. If you use the following statement to compile:

Javac -d D: / CJM D: /TEMP/TESTFILE.JAVA

The directory D: / CJM will automatically follow the directory structure specified in Packagek, and will be generated in this new directory, which will be created under this new directory. / TEST directory, then generated Testfile.class placed in the D: / CJM / EDU / TEST directory.

5. In order to facilitate the project, you can make your own tree into .jar files. If all types of files below the EDU in Figure 1. You can go to the D: / CJM directory first, then use the following command:

Jar -cvf Test.jar EDU /

At this time, a Test.jar file is generated under D: / Test, this .jar file contains the full directory structure and files under the EDU /. When using this .jar file, simply specify the .jar file in the ClassPath.

6. Use of other resources, such as icon files, text and other resource files must be noted that the search resource file should not start from the directory where the class file is located, but should start from the starting point of the class path specified by the package (Figure 1) Start with the directory where the EDU is located. As seen in Figure 1, Word.txt is under Resource, and the class file testfile.class Under EDU / TEST, you want to use Word.txt in the TestFile.class, you want to follow: FIN = NEW FileReader ("Resource / Word.txt"); not: fin = new fileReader ("../../ resource / word.txt");

Fourth, an example

This example is used to count the number of words in a text file, the number in the comment corresponds to the previous section number:

//Testfile.java package edu.test; // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - 4 Import java.io.fileReader; // ---------------------------- 3 Import java.io.LinenumberReader; Class Testfile {public static void main (string [] argv) {teststring ts = new teststring (); // ---------------- 2 FileReader Fin; LINENUMBERREADER line = null; int Wordnum = 0; Try {FIN = New FileReader ("Resource / Word.txt"); // ---- 6 line = new LineNumberReader (FIN);} catch (Exception E) {E.PrintStackTrace (); system.exit (0);} while (TRUE) {Try {string Temp = line.readline (); Wordnum = Ts.countword (TEMP);} catch (exception e) {Break;}} try {line.close (); } Catch (Exception E) {}; system.out.println ("Word Count Is:" Wordnum);}}

/TestString.java package edu.test; import java.util. *; Class teststring {int countword (string stringtokenizer token = new stringtokenizer (str); return token.countToKens ();}}

Two .java files are stored in the D: / TEMP directory, the current directory is D: / TEMP to compile the following command:

D: / Temp> Javac-ClassPath D: /jdk1.4.2/lib -d D: / Test * .java

Run with the following command:

// -------------------------------- 1 D: / TEMP> java -classpath.; D: / JDK1 .4.2 / lib; d: / test / com edu.test.testfile

If you need to pack it, go to D: / Test first, then use the following command:

// --------------------------------- 5 Jar -cvf test.jar EDU / At this time you can generate a Test .jar file, you can use this file to use it under any platform.

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

New Post(0)