将Java程序打包成EXE程序

news2024/9/21 14:26:16

Java制作可执行jar

方式一:mainClass与lib分离

1)将Java程序依赖的所有jar都拷贝在lib目录下,并添加到classpath中
2)运行时指定MainClass

pom.xml

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

<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脚本

@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

 <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运行时会多弹出一个框。

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

; 脚本由 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需要拷贝到相对路径的位置

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2152560.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

焦化行业的变革力量:智能巡检机器人

根据相关数据&#xff0c;2024年1-2月份&#xff0c;焦炭产量为8039.5万吨&#xff0c;同比增长2.1%&#xff0c;这表明&#xff0c;我国焦化行业仍是全球最大的焦炭生产国和消费国&#xff0c;其市场规模占据了重要地位。焦化企业主要集中在山西省&#xff0c;其合计焦炭产能约…

基础漏洞——SSRF

目录 一.原理 二.引起ssrf的函数 三.这些函数具体作用 &#xff08;1&#xff09;File_get_content() &#xff08;2&#xff09;Fsockopen() &#xff08;3&#xff09;Curl_exec() 四.常见的业务场景&#xff08;可能出现的漏洞的地方&#xff0c;漏洞挖掘&#xff09…

FFmpeg开发笔记(五十六)使用Media3的Exoplayer播放网络视频

Android早期的MediaPlayer控件对于网络视频的兼容性很差&#xff0c;所以后来单独推出了Exoplayer库增强支持网络视频&#xff0c;在《Android Studio开发实战&#xff1a;从零基础到App上线(第3版)》一书第14章的“14.3.3 新型播放器ExoPlayer”就详细介绍了Exoplayer库的详细…

stack和queue(一)

接下来讲解一些stack栈和queue的简单使用 stack的概念 stack是一种容器适配器&#xff0c;专门用在具有后进先出操作的上下文环境中&#xff0c;其删除只能从容器的一端进行 元素的插入与提取操作。 特性是先进先出 后进后出 构造一个栈堆 int main() {deque<int>…

树莓派配置Qt+OpenCV

本次教程使用的树莓派镜像&#xff1a;树莓派镜像带图像界面下载 Qt的安装&#xff1a; 在命令行依次输入以下命令安装Qt&#xff1a; sudo apt-get updatesudo apt-get upgrade sudo apt-get install qtbase5-dev qtchooser sudo apt-get install qt5-qmake qtbase5-dev-t…

threejs加载高度图渲染点云,不支持tiff

问题点 使用的point来渲染高度图点云&#xff0c;大数据图片无效渲染点多&#xff08;可以通过八叉树过滤掉无效点增加效率&#xff0c;这个太复杂&#xff09;&#xff0c;但是胜在简单能用 效果图 code 代码可运行&#xff0c;无需npm <!DOCTYPE html> <html la…

MySQL聚合统计和内置函数

【数据库】MySQL聚合统计 王笃笃-CSDN博客https://blog.csdn.net/wangduduniubi?typeblog显示平均工资低于2000的部门和它的平均工资 mysql> select deptno,avg(sal) deptavg from emp group by deptno; --------------------- | deptno | deptavg | --------------…

0x08 MotionEye 视频监控组件 list 信息泄漏洞 CVE-2022-25568

参考&#xff1a; MotionEye 视频监控组件 list 信息泄漏洞 CVE-2022-25568 | PeiQi文库 (wgpsec.org) 一、漏洞描述&#xff1a; motionEye是用Python写的motion的Web前端&#xff0c;它可以监视视频信号并检测运动。它可以与多种类型的摄像机配合使用,也可以与电影文件一起…

PMP--二模--解题--41-50

文章目录 11.风险管理--风险代表对将来问题的预判&#xff0c;问题代表对过去问题事件的跟踪&#xff1b;两者联系&#xff1a;风险发生后会变成问题&#xff0c;而问题可能导致新的风险。41、 [单选] 在项目会议期间&#xff0c;一个团队发现三个月前关闭的问题仍然处于活跃状…

解决 Prettier ESLint 错误

解决 Prettier ESLint 错误 在 Vue.js 项目中使用 ESLint 和 Prettier 时&#xff0c;你可能会遇到类似以下的错误&#xff1a; frontend\src\views\dashboard\MobileConfigPanel.vue1:25 error Delete ␍ …

使用IDA Pro动态调试Android APP

版权归作者所有&#xff0c;如有转发&#xff0c;请注明文章出处&#xff1a;https://cyrus-studio.github.io/blog/ 关于 android_server android_server 是 IDA Pro 在 Android 设备上运行的一个调试服务器。 通过在 Android 设备上运行android_server&#xff0c;IDA Pro …

SpringBoot项目同时集成Mybatis和Mybatis-plus框架

1. 背景 Mybatis-plus可以生成CRUD&#xff0c;减少开发中SQL编写量&#xff0c;但是某些情况下我们需要多表关联查询&#xff0c;这时候Mybatis可以手写SQL的优势就体现出来了&#xff0c;在实际开发中&#xff0c;项目里面一般都是Mybatis和Mybatis-Plus公用&#xff0c;但是…

【Geoserver使用】Geoserver 3前瞻

文章目录 前言一、GeoServer 3 Call for Crowdfunding&#xff08;GeoServer 3 呼吁众筹&#xff09;二、Geoserver 3升级内容1.升级到3的几个原因2.Geoserver 3的四个升级方向 总结 前言 今天来看看最近Geoserver官方发布的关于Geoserver 3重大升级众筹这篇官方博客中提到的几…

漫步者头戴式耳机怎么样?漫步者、西圣、索尼三大耳机测评对比

自头戴式耳机诞生以来&#xff0c;凭借其出色的音质表现和时尚造型&#xff0c;迅速赢得了音乐爱好者的青睐。头戴式耳机不仅能够带来更加沉浸的听觉体验&#xff0c;还具备较强的降噪功能&#xff0c;让用户在嘈杂环境中依然能专注于音乐世界。 与入耳式耳机相比&#xff0c;…

【PyVista】网状结构,标和单元[mesh,point,cell]的介绍

[PyVista] 介绍-CSDN博客中介绍给pyvista的介绍和简单的使用。接下来看看mesh的使用。 一&#xff0c;什么是网格? 在PyVista中&#xff0c;网格是任何空间引用信息&#xff0c;通常由三维空间中的表面或体积的几何表示组成。我们通常将任何空间引用的数据集称为网格&#xf…

回归预测 | Matlab实现SSA-HKELM麻雀算法优化混合核极限学习机多变量回归预测

回归预测 | Matlab实现SSA-HKELM麻雀算法优化混合核极限学习机多变量回归预测 目录 回归预测 | Matlab实现SSA-HKELM麻雀算法优化混合核极限学习机多变量回归预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实现SSA-HKELM麻雀算法优化混合核极限学习机多变量…

【java】常见限流算法原理及应用

目录 前言 限流的作用 4种常见限流算法 固定窗口限流 基本原理 简单实现 优点和缺点 滑动窗口限流 基本原理 简单实现 优点和缺点 漏桶限流 基本原理 简单实现 优点和缺点 令牌桶限流 基本原理 简单实现 优点和缺点 算法比较与选择 前言 在现代分布式系统…

102.SAPUI5 sap.ndc.BarcodeScannerButton调用摄像头时,localhost访问正常,使用IP访问失败

目录 原因 解决办法 1.修改谷歌浏览器的setting 2.在tomcat中配置https访问 参考 使用SAPUI5的sap.ndc.BarcodeScannerButton调用摄像头时&#xff0c;localhost访问正常&#xff0c;使用IP访问时&#xff0c;一直打不开摄像头&#xff0c;提示getUserMedia()问题。 原因…

【React】(推荐项目)一个用 React 构建的 CRUD 应用程序

推荐项目&#xff1a;CRUD 应用示例 在本篇文章中&#xff0c;我想向大家推荐一个非常实用的项目&#xff1a;CRUD 应用示例。这个项目展示了如何使用现代技术栈创建一个基础的增删改查&#xff08;CRUD&#xff09;应用&#xff0c;非常适合用于学习和实践后端开发技能。 适…

【工具变量】科技金融试点城市DID数据集(2000-2023年)

时间跨度&#xff1a;2000-2023年数据范围&#xff1a;286个地级市包含指标&#xff1a; year city treat post DID&#xff08;treat*post&#xff09; 样例数据&#xff1a; 包含内容&#xff1a; 全部内容下载链接&#xff1a; 参考文献-pdf格式&#xff1a;https://…