Fully master the "package" mechanism in Java

xiaoxiao2021-03-06  44

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: / Test / COM

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

figure 1

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: / Test / COM

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: /test/com/edu/test/testfile.java

Java-ClassPath.; D: /JDK1.4.2/lib; D: / Test / Com 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 that 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 program to 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: /test/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: / Test D: /test/edu/test/testfile.java

The directory will be created in the directory D: / Test specified after -D, and the directory will be created in accordance with the directory structure specified in Packagek, and the generated .class file will be placed in this new directory, that is, set up / EDU below D: / Test / TEST directory, then generated testfile.class placed in the D: / Test / EDU / TEST directory.

5. In order to facilitate the project, you can make your own tree into .jar files. If all class files below the EDU in Figure 1 are pamped into one .jar file, you can go to the D: / Test directory, 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.

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; // -------------------------------------- 4IMPORT 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 ("Word.txt");

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 Str) {

StringTokenizer token = New StringTokenizer (STR);

Return token.countToKens ();

}

}

Two .java files are stored in the D: / TEMP directory, the current directory is d: Temp is compiled using the following command:

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

Run with the following command:

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

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

Jar -cvf test.jar edu / // ---------------------------------------- ----------- 5

At this point, a Test.jar file can be generated, and this file can be placed under any platform.

(Please contact chenjm2000@hotmail.com if you need to discuss the issues involved.

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

New Post(0)