When a Java application is running, it needs to load the bytes of the classes used into the memory. Therefore, there are certain requirements for the location of the bytecode files, which are generally divided into the following four situations:
1. Use a class without a package name in the directory where the current application is located:
Classes that do not have a package name in the directory where the current application is located can be loaded and used directly.
2. Use classes in the class library in the Java runtime environment:
The classes in the class library provided by the Java runtime environment all have package names, and the application must use the import statement to introduce the classes in the corresponding package.
3. Use classes from directories that are descendants of the application's current directory:
The descendant directories under the current directory of the application can be used as the package name of the user-defined package. Classes with this package name must be stored in these descendant directories. The application can use the import statement to introduce classes in the user-defined package.
4. Use classes in the Java runtime environment extension:
We can use the jar.exe command to compress the bytecode files of some classes into a jar file, and then store the jar file in the extension of the Java runtime environment, that is, store the jar file in jrelibext of the JDK installation directory. folder, so that Java applications can use the classes in this jar file to create objects.
class with package name
Assume that the package name of the following TestOne and TestTwo classes is moon.star.
TestOne.java
packagemoon.star;//Package statement publicclassTestOne{publicvoidfTestOne(){System.out.println(IamamethodInTestOneclass);}}
TestTwo.java
packagemoon.star;//Package statement publicclassTestTwo{publicvoidfTestTwo(){System.out.println(IamamethodInTestTwoclass);}}
Save TestOne.java and TestTwo.java to the C:1000moonstar directory, and then enter this directory to compile the two source files respectively.
Now, we will compress TestOne.class and TestTwo.class in the C:1000moonstar directory into a jar file: Jerry.jar.
First, we write a manifest file: hello.mf (Manifestfiles).
hello.mf
Manifest-Version:1.0Class:moon.star.TestOnemoon.star.TestTwoCreated-By:1.6
Save hello.mf to the C:1000 directory. In order to use the jar command to generate a jar file, you first need to enter the C:1000 directory, that is, enter the directory above the package name, and then use the jar command to generate A file named Jerry.jar.
C:1000>jarcfmJerry.jarhello.mfmoonstarTestOne.classmoonstarTestTwo.class
If C:1000moonstar only has two bytecode files, TestOne.class and TestTwo.class, you can also use the jar command.
C:1000>jarcfmJerry.jarhello.mfmoonstar*.class
Finally, copy the Jerry.jar file generated by the jar command in the C:1000 directory to the extension of the Java runtime environment, that is, store the Jerry.jar file in the jrelibext folder of the JDK installation directory.
Class without package name
If the TestOne and TestTwo classes do not have package names, just save TestOne.java and TestTwo.java to C:1000 and compile the bytecode files. Write a manifest file, then save it to C:1000, enter the C:1000 directory, and use the jar command.
C:1000>jarcfmJerry.jarhello.mfTestOne.classTestTwo.class
If C:1000 only has two bytecode files, TestOne.class and TestTwo.class, you can also use the jar command.
C:1000>jarcfmJerry.jarhello.mf*.class
Finally, copy the Jerry.jar file generated by the jar command in the C:1000 directory to the jrelibext folder in the JDK installation directory. The application can directly use TestOne.class and TestTwo in the Jerry.jar file. .class.