Compress the *.class file into a file and deliver it to the user, then such a file is called a jar file. If you want to generate a jar file, you can directly use jar.exe in the bin directory in the JDK to compress all class files. This command is installed with the JDK. Enter jar directly on the command line to see the prompt operation of this command.
#1. First of all, there are many steps to install JDK, which will not be discussed here. Only the parts related to packaging will be discussed. Open the console interface, enter jar and press Enter to view the operation command prompts related to jar packaging.
#2. In order to make it easier for everyone to understand, only a single class is packaged here. The picture below shows the java source code written, just to show the packaging steps.
#3. Then compile the program to generate a class file, and then package the generated com folder.
#4. It needs to be explained here that the source file when packaging, that is, the Demo.java file does not need to be entered, only the *.class file needs to be entered. In the console, enter the file directory where the com file is located, such as the javapro file in this example. As shown in the figure
5. After entering the javapro file, enter jar -cvf [name of the jar package] [file to be packaged].
(Related video tutorial sharing: java video tutorial)
6. For example, jar -cvf my.jar com, the effect is as follows Illustration.
#7. Finally, how to use this jar package. To use the jar package, you need to set environment variables, which can be set on the computer or with commands on the console. The latter is used here.
Enter in the consoleset classpath=.;d:\javapro\my.jar
8. Write a test class below to test whether this jar package is available. Before testing, delete the generated Demo.class file and keep only the jar package. The test program is shown in the figure below.
#9. Compile and run TestDemo.java on the console, and the effect is as shown in the figure.
The above is the detailed content of How to generate jar in java. For more information, please follow other related articles on the PHP Chinese website!