A question about how IDEA packages runnable jars.

scene:

Sometimes, we use IDEA to develop some small tools, which need to be packaged as runnable JAR packages; or some projects are not web applications, but purely background applications. When publishing, they also need to be packaged as runnable JAR packages. Moreover, if you rely on third-party jars, you do not want the third-party jars to be included in the final runnable jar package, and you also want to organize the directory structure (such as placing third-party jar files in parallel with the final runnable jar package). directory lib)

 

development environment

OS : Windows 8.1

JDK: 1.7.0

Development tools: IntelliJ IDEA 13.1.1

 

step:

1. Select the project name of the Java project and select  File -> project structure ...  in the menu (shortcut Ctrl+Alt+Shift+S ).



 

2. In the pop-up window, select " Artifacts " on the left, click " + " to select jar, and then select " from modules with dependencies ".



 

3. Configure the " Main Class " in the configuration window .



 

4. After selecting "Main Class", select "copy to the output and link via manifest", and configure "Directory for META-INF/MAINFEST.MF". The default value of this configuration is: D:\workshop\DbUtil\ src\main\java, need to be changed to: D:\workshop\DbUtil\src\main\resources, if not changed, the jar package will not contain META-INF/MAINFEST.MF file, this should be an IDEA BUG (Reference: http://stackoverflow.com/questions/15724091/how-to-run-a-jar-file-created-using-intellij-12 ), after the configuration is completed, as shown in the figure below, click OK to go to the next step . (If you choose "extract to the target jar", that is, when you put the third-party jar file into the final runnable jar package, you do not need to modify the configuration of "Directory for META-INF/MAINFEST.MF", use the default value that is can )


 

5. Right-click "<output root>", select "Create Directory", and enter the directory name: lib



 

6. Hold down the third-party jar with the mouse and drag it into the lib directory under <output root>



 

7. Use the mouse to select the jar package file we are going to print, click the button behind "Class Path:" below, and in the "Edit Classpath" window, add the relative path: lib\ before each third-party jar file to form a file like : in the form of lib\xxx.jar, this directory "lib" is the "lib" entered earlier, if there are many third-party jars here, you can use the mouse to select all third-party jars in the "Edit Classpath" window, and then Ctrl+X, cut & copy the content to UltraEdit, modify it all at once with the column format, and paste it into the "Edit Classpath" window. When finished it looks like this:


 

8. After completion, click OK, Apply and other buttons to return to the main menu of IDEA, select "Build" or "Rebuild" under "Build - Build Artifacts" to generate the final runnable jar, and put the third-party jar Put it in the lib directory parallel to this jar, open the target jar with winrar, you can see that the META-INF/MAINFEST.MF file is correctly included and the content is correct.


 

9. In the output directory of the jar package, execute the command: java -jar xxx.jar to run the jar file.

 


 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327075655&siteId=291194637