=Start=
缘由:
近期由于工作需要,又打开了电脑上沉睡已久的IDEA编辑器,写一些简单的Java代码做联通和功能性验证,在验证的时候除了直接在IDEA里运行之外,还需要将文件放到IDC服务器上面去跑(在生产环境中进行验证),这时就需要生成可执行jar包了,之前也操作过,但用的不多所以总忘,这次趁机整理一下,方便以后需要的时候进行参考。
正文:
参考解答:
方法一:除了IDEA不用额外依赖
ps: 没有提到的地方就是默认选择,直接点击next就行。
- 打开 Artifacts 工具
File
-> Project Structure
-> Project Settings
-> Artifacts
-> Click plus sign(点击+号按钮) -> Jar
-> From modules with dependencies…
(胖包)
- 进入打包界面,设置主类和文件保存位置
Main Class
JAR Files from libraries: extract to the target JAR
…
- Build-构建
Build -> Build Artifacts -> 选择刚才创建的Artifact -> Build
- 找到刚才编译构建出的文件
ProjectName | out | artifacts | ProjectName_jar | ProjectName.jar
方法二:借助Maven插件
- 在pom.xml中添加相关插件的Maven依赖
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
</dependency>
- 在pom.xml中的插件部分添加相关插件
<!-- 注意:插入在build下,而非pluginManagement下 -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.0</version>
<configuration> <!--这里的版本可以注释掉,也可以按照自己的实际环境进行填写-->
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>fatjar</descriptorRef> <!--这部分内容会体现在生成的jar包文件名上-->
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.example.TestMySQL</mainClass> <!--这里改成自己的主类位置-->
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
- 在右侧的Maven工具中进行package打包
Maven -> Lifecycle -> package
- 找到刚才生成的jar包
在target文件夹中。
参考链接:
How to build jars from IntelliJ properly? #verified,纯文字版,对新手来说没那么友好,但胜在简单直接
https://stackoverflow.com/questions/1082580/how-to-build-jars-from-intellij-properly
IDEA导出可执行jar包 #verified
https://blog.csdn.net/ouyang111222/article/details/73105086
IDEA系列:idea打包生成可执行jar包
https://blog.51cto.com/NIO4444/3842569
idea打包java可执行jar包
https://www.cnblogs.com/blog5277/p/5920560.html
IDEA中如何正确快速打jar包(包括瘦包、胖包) #verified
https://blog.csdn.net/qq_41704237/article/details/107032965
打胖瘦jar包的两种方式(idea与maven插件),多图详解 #verified
https://blog.csdn.net/qq_45363033/article/details/106639601
How can I create an executable JAR with dependencies using Maven?
https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven
Can’t execute jar- file: “no main manifest attribute”
https://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute
Apache Maven Assembly Plugin
https://maven.apache.org/plugins/maven-assembly-plugin/index.html
https://maven.apache.org/plugins/maven-assembly-plugin/examples/sharing-descriptors.html
maven带依赖打包成jar
https://blog.51cto.com/u_15119353/3303718
=END=
《“如何借助IDEA快速生成可执行jar包”》 有 1 条评论
How to run a class from Jar which is not the Main-Class in its Manifest file
https://stackoverflow.com/questions/5474666/how-to-run-a-class-from-jar-which-is-not-the-main-class-in-its-manifest-file
`
Execute Main-Class:
$ java -jar MyJar.jar // will execute the Main-Class
Execute another class with a public static void main method:
$ java -cp MyJar.jar com.mycomp.myproj.AnotherClassWithMainMethod
Note: the first uses -jar, the second uses -cp.
`
Run a Java Application from the Command Line
https://www.baeldung.com/java-run-jar-with-arguments