将Java程序打包成EXE程序

Java制作可执行jar

方式一:mainClass与lib分离

1)将Java程序依赖的所有jar都拷贝在lib目录下,并添加到classpath中

2)运行时指定MainClass

pom.xml

这个pom.xml生成的jar可双击直接运行,但是因为没有将其依赖的jar都打包在一起,因此用到其他jar的时候会报错,可通过脚本将其他jar添加到环境变量,以脚本启动。

bash 复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.studio</groupId>
  <artifactId>GenLicense</artifactId>
  <version>1.0</version>
  <properties>
        <java.version>1.8</java.version>
        <!-- 工具类相关 -->
        <hutool.version>5.8.23</hutool.version>
       
         <!-- Maven 相关 -->
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <git-build-hook-maven-plugin.version>3.4.1</git-build-hook-maven-plugin.version>
        <!-- 项目编码相关 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
         <dependency>
            <groupId>cn.com.infosec</groupId>
            <artifactId>licence</artifactId>
            <scope>system</scope>
            <version>1.0</version>
            <systemPath>${basedir}/lib/licence.jar</systemPath>
        </dependency>
         <dependency>
            <groupId>com.formdev</groupId>
            <artifactId>flatlaf</artifactId>
            <version>3.4.1</version>
        </dependency>
    </dependencies>
   <build>
        <plugins>
            <!-- 打包时跳过测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                  <archive>
                    <manifest>
                      <addClasspath>true</addClasspath>
                      <mainClass>com.studio.MainFrame</mainClass>
                    </manifest>
                  </archive>
                </configuration>
              </plugin>
        </plugins>
        <resources>
        <resource>
            <directory>src/main/resources</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>


bat脚本

bash 复制代码
@echo off
title GenLicense

SetLocal EnableDelayedExpansion
set JAVA_HOME="./jdk1.8.0_144"

set PATH=%JAVA_HOME%\bin;%PATH%

set CLASSPATH="%CLASSPATH%;.;./lib/*;"

echo %CLASSPATH% 

rem 启动Java服务

%JAVA_HOME%\bin\java com.studio.MainFrame

EndLocal
pause

方法二:使用SpringBoot插件将所有依赖都打入同一个jar包

pom.xml

bash 复制代码
 <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     <version>2.7.18</version>
     <executions>
         <execution>
             <goals>
                 <goal>repackage</goal>
             </goals>
         </execution>
     </executions>
     <configuration>
         <mainClass>com.studio.MainFrame</mainClass>
         <includeSystemScope>true</includeSystemScope>
     </configuration>
 </plugin>


exe4j生成EXE

注:需要先将exe4j激活,否则生成的exe运行时会多弹出一个框。

bash 复制代码
https://exe4j.apponic.com/














注:

1)上述生成exe并运行,本质原理是<Java制作可执行jar的方法一>,方法二不行,因为SpringBoot打出来的jar没有指定mainClass的地方。

2)使用exe4j生成的exe可以直接运行,但需要依赖JDK环境,在search sequence步骤,jdk要以相对路径填入,exe4j并不会将jdk打包,我们借助inno setup将jdk打包到相对路径的位置。

Inno Setup将JDK打入安装包













Inno打包脚本.iss

bash 复制代码
; 脚本由 Inno Setup 脚本向导 生成!
; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档!

#define MyAppName "License生成器"
#define MyAppVersion "1.0"
#define MyAppPublisher "我的公司"
#define MyAppURL "http://www.example.com/"
#define MyAppExeName "GenLicense.exe"

[Setup]
; 注: AppId的值为单独标识该应用程序。
; 不要为其他安装程序使用相同的AppId值。
; (若要生成新的 GUID,可在菜单中点击 "工具|生成 GUID"。)
AppId={{C6B0667C-0C65-49E7-BEC4-30458967D645}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppName}
DisableProgramGroupPage=yes
; 以下行取消注释,以在非管理安装模式下运行(仅为当前用户安装)。
;PrivilegesRequired=lowest
OutputDir=./output2
OutputBaseFilename=GenLicense
SetupIconFile=./icon.ico
Compression=lzma
SolidCompression=yes
WizardStyle=modern

[Languages]
Name: "chinesesimp"; MessagesFile: "compiler:Default.isl"

[Tasks]
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked

[Files]
Source: "E:\Exe4j\output\GenLicense.exe"; DestDir: "{app}"; Flags: ignoreversion
Source: "C:\Users\user\Desktop\GenLicense\多jar包运行\jdk1.8.0_144\*"; DestDir: "{app}\jdk"; Flags: ignoreversion recursesubdirs createallsubdirs
; 注意: 不要在任何共享系统文件上使用"Flags: ignoreversion"

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"
Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: desktopicon

[Run]
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent

注意

1)exe4j生成可执行exe时,需要指定EXE运行时JDK的相对路径

2)inno打包时,JDK需要拷贝到相对路径的位置

相关推荐
长安初雪2 分钟前
Java客户端SpringDataRedis(RedisTemplate使用)
java·redis
繁依Fanyi21 分钟前
使用 Spring Boot + Redis + Vue 实现动态路由加载页面
开发语言·vue.js·pytorch·spring boot·redis·python·算法
aloha_78927 分钟前
B站宋红康JAVA基础视频教程(chapter14数据结构与集合源码)
java·数据结构·spring boot·算法·spring cloud·mybatis
星尘安全28 分钟前
一种新的电子邮件攻击方式:AiTM
开发语言·网络安全·php·网络钓鱼·aitm
尘浮生36 分钟前
Java项目实战II基于Java+Spring Boot+MySQL的洗衣店订单管理系统(开发文档+源码+数据库)
java·开发语言·数据库·spring boot·mysql·maven·intellij-idea
鸽芷咕37 分钟前
【Python报错已解决】xlrd.biffh.XLRDError: Excel xlsx file; not supported
开发语言·python·机器学习·bug·excel
铁匠匠匠1 小时前
【C总集篇】第八章 数组和指针
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
猿饵块1 小时前
cmake--get_filename_component
java·前端·c++
编程小白煎堆1 小时前
C语言:枚举类型
java·开发语言
秋邱1 小时前
C++: 类和对象(上)
开发语言·c++