You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Maven构建含嵌入式JRE的项目?适配无预安装JRE环境

我之前刚好处理过几乎一样的需求,给你分享下落地的具体步骤,完全适配你当前在用的Maven插件组合:

实现嵌入式JRE打包的完整方案

第一步:准备嵌入式JRE

  • 先下载对应目标运行平台的JRE(比如Windows x64、Linux x64),解压到项目的src/main/resources/java8目录下(和你设想的路径一致)。
  • 建议精简JRE体积:Java 8可以手动删除demo、sample、多余locale文件夹;Java 9+推荐用jlink生成只包含你项目依赖模块的自定义JRE,能大幅缩小包大小。

第二步:调整Maven插件配置

1. maven-jar-plugin:指定主类与类路径

要让程序能找到嵌入式JRE的类库,需要在Jar的MANIFEST里配置正确的类路径和主类:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>你的主类全路径(比如com.example.MyAppMain)</mainClass>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
            <manifestEntries>
                <!-- 显式指定嵌入式JRE的核心类库路径 -->
                <Class-Path>. java8/lib/rt.jar java8/lib/jce.jar</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

2. maven-assembly-plugin:打包所有资源

创建一个自定义的assembly描述符文件src/main/assembly/embedded-jre-assembly.xml,用来把JRE、Jar包、本地依赖、启动脚本全部打包到一起:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>embedded-jre-package</id>
    <formats>
        <format>zip</format>
        <format>dir</format> <!-- 可选:生成可直接运行的目录结构 -->
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <!-- 复制嵌入式JRE到打包根目录 -->
        <fileSet>
            <directory>src/main/resources/java8</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
        </fileSet>
        <!-- 复制编译好的Jar包 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>${project.build.finalName}.jar</include>
            </includes>
        </fileSet>
        <!-- 复制本地依赖(对应你用的maven-nativedependencies-plugin输出的文件) -->
        <fileSet>
            <directory>${project.build.directory}/native-libs</directory>
            <outputDirectory>/lib</outputDirectory>
            <includes>
                <include>**/*.dll</include>
                <include>**/*.so</include>
                <include>**/*.dylib</include>
            </includes>
        </fileSet>
        <!-- 复制启动脚本 -->
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>start.bat</include>
                <include>start.sh</include>
            </includes>
            <fileMode>0755</fileMode> <!-- 给shell脚本加执行权限 -->
        </fileSet>
    </fileSets>
</assembly>

然后在pom.xml里配置assembly插件使用这个描述符:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.6.0</version>
    <executions>
        <execution>
            <id>build-embedded-package</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/embedded-jre-assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

3. 编写启动脚本(核心!)

直接运行Jar会调用系统JRE,所以必须写脚本指定用嵌入式JRE的java命令启动程序:

  • Windows平台start.bat(放到src/main/resources):
@echo off
setlocal
set JAVA_HOME=%~dp0java8
set PATH=%JAVA_HOME%\bin;%PATH%
java -jar %~dp0${project.build.finalName}.jar
endlocal
  • Linux/Mac平台start.sh(放到src/main/resources):
#!/bin/bash
DIR=$(cd "$(dirname "$0")" && pwd)
JAVA_HOME="$DIR/java8"
PATH="$JAVA_HOME/bin:$PATH"
java -jar "$DIR/${project.build.finalName}.jar"

第三步:测试打包与运行

执行mvn clean package后,会在target目录生成带嵌入式JRE的zip包(或目录)。把这个包解压到没有安装JRE的目标系统,运行对应平台的启动脚本,程序就能正常启动了。

额外注意事项

  • 如果要支持多平台,可以用Maven Profile分别打包对应平台的JRE版本。
  • Java 8的JRE精简时注意不要删除核心类库,比如rt.jar、jce.jar这些必须保留的文件。

内容的提问来源于stack exchange,提问作者LOLWTFasdasd asdad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:46:47