java 如何使用便携式 JRE 打包可执行 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/664858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:18:25  来源:igfitidea点击:

How can you package an executable jar with a portable JRE?

javaexecutable-jar

提问by Penchant

I want to be able to distribute by executable JAR without having to make the user upgrade to the latest version of the JRE. How can I package a JRE into the JAR so that they can run the jar with no JRE installed on their system?

我希望能够通过可执行 JAR 进行分发,而不必让用户升级到最新版本的 JRE。如何将 JRE 打包到 JAR 中,以便他们可以在系统上未安装 JRE 的情况下运行 jar?

回答by Penchant

Put Java Portablein a sub-directory and launch your jar using a batch/bash script or Launch4j.

Java Portable放在子目录中,然后使用批处理/bash 脚本或Launch4j启动您的 jar 。

回答by Maurice Perry

As you need a jre to execute a jar, I'm afraid this is not possible.

由于您需要 jre 来执行 jar,恐怕这是不可能的。

I have once used izpacktogether with launch4jto make a Windows EXE that would install a jre with my application.

我曾经使用的IzPack连同launch4j以使Windows EXE,将安装了JRE我的申请。

回答by eniel.rod

Ten years later I ran into the same problem, and after several days looking, apparently there is still no solution, this is the best thing I have found:

十年后我遇到了同样的问题,看了几天,显然仍然没有解决方案,这是我发现的最好的事情:

Bundle Java (the JRE) and Launch a Java App with 7zip SFX: Convert Java Apps to an Executable https://mark.koli.ch/bundle-java-the-jre-and-launch-a-java-app-with-7zip-sfx

捆绑 Java(JRE)并使用 7zip SFX 启动 Java 应用程序:将 Java 应用程序转换为可执行文件https://mark.koli.ch/bundle-java-the-jre-and-launch-a-java-app-with -7zip-sfx

Edit

编辑

I found another way to do it, based on Inno Setup and NetBeans, it often appears as a result of searches about converting jar to exe. Everything is done using Native Packaging in NetBeans IDE.

我找到了另一种方法,基于 Inno Setup 和 NetBeans,它经常出现在搜索将 jar 转换为 exe 的结果中。一切都是在 NetBeans IDE 中使用本机打包完成的。

The result is a standalone .exe installer (YourProjectName.exe) that creates a folder in AppData\Local\YourProjectName. In that folder you will find the launcher, which is an .exe, and also all the necessary files to run the software without installing Java.

结果是一个独立的 .exe 安装程序 (YourProjectName.exe),它在AppData\Local\YourProjectName中创建一个文件夹。在该文件夹中,您将找到启动程序,它是一个 .exe,以及无需安装 Java 即可运行该软件的所有必要文件。

You should use exactly the same tools versions that says in the article, otherwise it does not work, at least not for me. The other thing that is not clear is that your project must be a Java with Ant proyect, at least I did not find how to do it with Gradle or Maven.

你应该使用文章中所说的完全相同的工具版本,否则它不起作用,至少对我来说不是。另一件不清楚的是,你的项目必须是带有 Ant 项目的 Java,至少我没有找到如何用 Gradle 或 Maven 来做。

Also, I had to install an old i586version of Java (I tried JDK 1.8 i586 and it worked) because JDK 13 causes errors (missing dll's) when trying to run the software. Although I only tried with JDK 13 x64, maybe architecture is the problem and not the version. Whatever, similar happened to NetBeans 11, the .exe launcher did not work, however when locating and executing the .jar it did work. So I ended up using NetBeans 8.

另外,我必须安装旧的i586版本的 Java(我尝试了 JDK 1.8 i586 并且它工作了),因为 JDK 13 在尝试运行该软件时会导致错误(缺少 dll)。虽然我只尝试过 JDK 13 x64,但也许架构是问题而不是版本。不管怎样,与 NetBeans 11 类似,.exe 启动器不起作用,但是在定位和执行 .jar 时它确实起作用了。所以我最终使用了 NetBeans 8。