Home >Java >JavaBase >How to package into jar package in java

How to package into jar package in java

王林
王林Original
2019-12-04 14:24:3023922browse

How to package into jar package in java

1. Manually package the directly executable jar package

1) First use javac to compile the java file and get the class file;

2) Create a new file with any name, for example, it can be called manifest, and the content is as follows (Note: 1. Add a space after the colon, 2. Finally, you must press Enter to a new blank line, otherwise an error will occur, as shown below. Enter another carriage return after addJarPkg);

Manifest-Version: 1.0<br>Main-Class: addJarPkg

3) Put the compiled class file and Step 2. Put the newly created file into the specified folder, such as test;

java video tutorial recommendation: java online tutorial

4) Run the command in cmd

jar -cvfm main.jar manifest -C test .

Pay attention to the path problem, where:

The "." after test represents all files, and the -m option after jar will merge the files created in step 2 into the in the jar package META-INF/MANIFEST.MF, which is the update manifest configuration file, -C specifies the directory to be packaged, and the . after the directory represents all files in the directory.

2. Use the intellij idea tool to package a directly executable jar package

1) Click on the project

2) Click on the "" in the upper left corner of intellij idea File" menu -> Project Structure

How to package into jar package in java

#3) Click "Artifacts" -> green " " -> "JAR" -> Empty

How to package into jar package in java

4) Fill in the custom name in the Name column, select the jar package target directory in Output dictory, and double-click the file that needs to be added to the jar package in Available Elements to add it to the jar package on the left directory, see the picture below for details:

How to package into jar package in java

5) Click Create Manifest and select the file path where MANIFEST.MF is placed (just default to the project root directory, try not to choose anything else path, may cause unnecessary errors), click OK

How to package into jar package in java

6) Click the select button behind Main Class

How to package into jar package in java

7) In the pop-up box, select the program entry main function that needs to be run, and click OK

How to package into jar package in java

8) After completing the above settings, click OK

9) Click the menu "Build" -> "Build Artifacts"

How to package into jar package in java

10) Double-click the build under the jar package to be generated in the pop-up box, as shown below:

How to package into jar package in java

At this point, use Intellij idea to generate a jar package that can be directly executed.

Recommended java related articles and tutorials:

java introductory tutorial

The above is the detailed content of How to package into jar package 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