【Swing】构建可执行Jar,通过命令执行Jar后自动关闭命令提示符窗口
=====================================================
1、运行Jar不显示命令提示符
2、Idea打Jar包
3、Maven打Jar包
=====================================================
1、运行Jar不显示命令提示符
创建 run.bat 文件,使用 start javaw.exe -jar my.jar 执行jar后关闭命令提示符
@echo off
start javaw.exe -jar my.jar
exit
2、Idea打Jar包
使用Build Artifacts打Jar包
File -> Project Structure... 或 Ctrl+Shift+Alt+S
Project Settings -> Artifacts -> +
Jar -> From modules with dependencies...
选择 Module, Main Class, JAR files from libraries, 填写 Directory for META-INF/MANIFEST.MF
填写 Name, Type, Output directory,然后确定
构建 Build -> Build Artifacts...
然后在工程 LargeTextRetrieval\target 目录下生成 LargeTextRetrieval.jar
3、Maven打Jar包
参考文档:https://blog.csdn.net/tekin_cn/article/details/136920305
使用Maven插件 maven-assembly-plugin 打Jar包
<build>
<plugins>
<!-- mvn clean package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<!-- 应用程序的入口类 -->
<mainClass>ycx.SwingMaven</mainClass>
</manifest>
</archive>
<descriptorRefs>
<!-- 预定义的描述符会将所有的依赖包含在最终的Jar包中 -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
执行命令
mvn clean package
要想仅保留assembly构建的Jar,可以跳过默认打包,<skip>true</skip>
<!-- 跳过默认jar打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<skip>true</skip>
<archive>
<manifest>
<!-- 应用程序的入口类 -->
<mainClass>ycx.SwingMaven</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 指定所有依赖的jar包的输出路径 -->
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>