简体   繁体   English

我如何在我的 JAR 中捆绑 JRE,以便它可以在没有 Java 的系统上运行?

[英]How do I bundle a JRE in my JAR, so that it can run on systems without Java?

I want to bundle a JRE inside my executable JAR, so that the exe can run on any system.我想在我的可执行 JAR 中捆绑一个 JRE,以便该 exe 可以在任何系统上运行。

I have tried Launch4j , but that approach requires me to ship both the JRE and the exe.我已经尝试过Launch4j ,但这种方法要求我同时提供 JRE 和 exe。 As per my requirements, I should not use an installer to ship, nor extract/install the JRE on the client machine.根据我的要求,我不应该使用安装程序来运送,也不应该在客户端计算机上提取/安装 JRE。

How do I put a JRE inside a JAR and make use of it?如何将 JRE 放入 JAR 中并使用它?

You cannot put a JRE inside a JAR file.您不能将 JRE 放入 JAR 文件中。 (Well you can... but it won't help.) (好吧,你可以......但它无济于事。)

What you need to do is build and distribute an installer.您需要做的是构建和分发安装程序。 The recommended way to create an installer is to use a commercial or open-source installer generator.创建安装程序的推荐方法是使用商业或开源安装程序生成器。 (Google will help you find one.) (谷歌会帮你找到一个。)

It is also possible to do embed a JRE in a ZIP file, as described here:也可以在 ZIP 文件中嵌入 JRE,如下所述:

The ZIP file contains the JRE and the application JAR, and other files that it needs. ZIP 文件包含 JRE 和应用程序 JAR,以及它需要的其他文件。 The user has to unzip the ZIP file to install the application.用户必须解压缩 ZIP 文件才能安装应用程序。


.. but it's copying total JRE in client system of almost 100 MB. .. 但它在客户端系统中复制了将近 100 MB 的总 JRE。 Is it possible to make JRE light weight?是否有可能使 JRE 轻量化?

Not really.并不真地。 The most (legal) light-weight way to distribute a JRE is to distribute an Oracle Java installer.分发 JRE 的最(合法)轻量级方法是分发 Oracle Java 安装程序。 Beware that the Java Binary License forbids the distribution of a cut-down JRE.请注意,Java 二进制许可证禁止分发精简的 JRE。 If you want to go down that path talk to a lawyer first!!如果您想走这条路,请先咨询律师!


Distributing Java apps with an embedded JRE is arguably a bad thing:分发带有嵌入式 JRE 的 Java 应用程序可以说是一件坏事:

  • It fills up the user's disk with multiple JREs.它用多个 JRE 填满了用户的磁盘。
  • The JREs tend to be hidden away / not visible to normal security updates / patching / audits. JRE 往往被隐藏起来/对正常的安全更新/补丁/审计不可见。 Hence they tend to become a security liability.因此,它们往往成为安全责任。
  • You need to regularly update your distributables... to avoid foisting out-of-date / insecure JREs on the unsuspecting user.您需要定期更新您的可分发文件……以避免将过时/不安全的 JRE 强加给毫无戒心的用户。

From Java 9 onwards there is a better solution than inluding a standard JRE in your installer.从 Java 9 开始,有一个比在安装程序中包含标准 JRE 更好的解决方案。 Use the new jlink utility to create a tailored JRE for your application;使用新的jlink实用程序为您的应用程序创建定制的 JRE; see https://www.baeldung.com/jlink for a tutorial, and the Oracle Jlink manual entry .有关教程和 Oracle Jlink 手册条目,请参阅https://www.baeldung.com/jlink

Note that jlink addresses the concerns with embedded JREs that we mentioned above.请注意, jlink解决了我们上面提到的嵌入式 JRE 的问题。 But it clearly makes it your responsibility 1 to provide JVM security patches to your users in the form of new jlink'd distributables.但这显然使您有责任1以新的 jlink 可分发文件的形式向您的用户提供 JVM 安全补丁。

1 - In reality, it has always been an application supplier / vendor's responsibility to advise and assist customers in upgrading embedded JREs. 1 - 实际上,建议和协助客户升级嵌入式 JRE一直是应用程序供应商/供应商的责任。 Unfortunately, many vendors neglected this, leading to customers running applications with known Java vulnerabilities, and Sun/Oracle getting the blame... unfairly... for the vendor's failings.不幸的是,许多供应商忽视了这一点,导致客户运行的应用程序具有已知的 Java 漏洞,而 Sun/Oracle 却……不公平地……因供应商的失败而受到指责。

It did work for me with launch4j它确实对我有用 launch4j

first create an app folder then open launch4j and type in the jar file, output file (should be in the app folder), and the icon (please have it in app folder).首先创建一个 app 文件夹,然后打开 launch4j 并输入 jar 文件、输出文件(应该在 app 文件夹中)和图标(请将它放在 app 文件夹中)。 then go to the jre part and type jre-{version ur using} in the bundled jre paths then put option only use private jdk runtimes then put the min version to ur java version, then go the jdk installation and copy the bin and lib folder then create a jre-{version ur using} folder then paste these in. Then compile it into exe!然后转到 jre 部分并在捆绑的 jre 路径中键入 jre-{version ur using} 然后将选项仅使用私有 jdk 运行时然后将最小版本放入您的 java 版本,然后进行 jdk 安装并复制 bin 和 lib 文件夹然后创建一个 jre-{version ur using} 文件夹,然后将这些粘贴进去。然后将其编译成 exe!

Late better than never.迟到总比不到好。 I used maven-shade-plugin in the pom.xml as follows:我在 pom.xml 中使用了 maven-shade-plugin,如下所示:

        <plugin>
          <artifactId>maven-shade-plugin</artifactId>
          <version>2.4.3</version>
          <configuration>
              <transformers>
                  <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                  <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>${mainClass}</mainClass>
                  </transformer>
              </transformers>
              <!-- If false the shaded artifact takes the normal artifact name, such as,  "core-tools-1.0-SNAPSHOT.jar"-->
              <!-- Meanwhile, another non-shaded version takes the prefix Original, such as  "Original-core-tools-1.0-SNAPSHOT.jar"-->
            <shadedArtifactAttached>false</shadedArtifactAttached>
              <!-- Exclude signed Manifests from the UberJar -->
              <filters>
                  <filter>
                      <artifact>*:*</artifact>
                      <excludes>
                          <exclude>META-INF/*.SF</exclude>
                          <exclude>META-INF/*.DSA</exclude>
                          <exclude>META-INF/*.RSA</exclude>
                      </excludes>
                  </filter>
                  <filter>
                      <artifact>*:*</artifact>
                      <includes>
                          <include>sun/misc/**</include>
                      </includes>
                  </filter>
              </filters>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                      <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打包 java jar 文件以在未安装 JRE 的情况下在 Mac 上运行? - How do I package a java jar file to run on a Mac without JRE installed? 如何用jar文件打包JRE,以便在Java独立机器上运行 - how to package JRE with jar file so as to run on Java independent machine 如何导出Java项目,使其无需IDE(netbeans)即可运行? - How do I export my java project so it can run without the help of the IDE (netbeans)? 如何将 jre8 与 springboot 应用程序捆绑在一起,以在安装了先前 Java 版本的服务器上运行它? - How can I bundle jre8 with springboot application to run it on server which has previous java version installed? 我有一个Java应用程序在JRE 6中运行,但不在JRE 7中运行。如何在JRE7中运行该应用程序 - I have a Java App running in JRE 6 but not in JRE 7. How do i run the application in JRE7 如何从与当前项目的JRE不同的JRE上的库jar中执行方法 - How do I execute a method from a library jar on a different JRE than my current project's JRE 如何在没有 Java JDK 的情况下运行 Runnable Jar 文件? - How do I run a Runnable Jar File without Java JDK? 仅使用JRE,如何在没有JDK的情况下能够编译和运行Java程序? - How can I able to compile and run java programs without JDK with just JRE? 如何检查我的Java应用程序将在具有JRE 1.6 / 1.7的计算机上运行? - How do I check that my Java application will run on a machine with JRE 1.6/1.7? 我怎么用jre编译.jar? - How do i compile .jar with jre?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM