Home  >  Article  >  Java  >  How to generate jar in java

How to generate jar in java

angryTom
angryTomOriginal
2020-02-03 14:52:384316browse

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.

How to generate jar in java

#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.

How to generate jar in java

#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.

How to generate jar in java

#3. Then compile the program to generate a class file, and then package the generated com folder.

How to generate jar in java

#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

How to generate jar in java

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)

How to generate jar in java

6. For example, jar -cvf my.jar com, the effect is as follows Illustration.

How to generate jar in java

#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.

How to generate jar in java

#9. Compile and run TestDemo.java on the console, and the effect is as shown in the figure.

How to generate jar in java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is jdk java?Next article:Is jdk java?