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
#3) Click "Artifacts" -> green " " -> "JAR" -> Empty 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: 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 6) Click the select button behind Main Class 7) In the pop-up box, select the program entry main function that needs to be run, and click OK 8) After completing the above settings, click OK9) Click the menu "Build" -> "Build Artifacts" 10) Double-click the build under the jar package to be generated in the pop-up box, as shown below: At this point, use Intellij idea to generate a jar package that can be directly executed. Recommended java related articles and tutorials: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!