请求JRE在启动java.exe时运行一个jar文件。

huangapple go评论75阅读模式
英文:

Asking JRE to run a jarfile on launching the java.exe

问题

我正在创建一个兼容Discord Rich Presence的游戏,由于Discord使用exe文件来启动游戏,所以当我尝试从Discord启动我的游戏时,它会启动来自JRE的java.exe可执行文件,而不是我的游戏jar文件。

我的问题是,是否有任何方法可以复制jre(将其与游戏捆绑)并设置它自动启动特定的jar文件?如果可以,该如何实现?
如果不可能,是否有任何方法可以在不单独启动java.exe的情况下从exe启动jar文件?
谢谢你的帮助。

  • 对不起,我的英语不太好,我是法国人
英文:

I'm creating a game compatible with Discord's Rich Presence, and as Discord uses exe files to launch games, when I'm trying to launch my game from Discord, it starts the java.exe executable coming from the JRE instead of the jarfile of my game.

What I'm asking is, is there any way to copy jre (to bundle it with the game) and setup it to start automatically a specific jarfile ? If yes, how ?
If it's not possible, is there any way to launch a jar from an exe without launching java.exe separately ?
Thanks for your help.

  • Sorry for bad English, I'm french

答案1

得分: 2

Java 14包含一个打包工具,允许您做到这一点。关于如何使用该工具的教程也是可用的,例如在这里。简而言之,像这样的代码应该可以工作:

jpackage --input target/ \
  --name MyApp \
  --main-jar myApp.jar \
  --main-class com.company.MyApp \
  --type exe \
  --java-options '--enable-preview';

如果您尚未使用Java 14而是使用较旧的版本,可以查看WinRun4J或者Launch4J。这两者都允许为Jar文件创建exe包装器,或者创建一个带有JRE和jar文件的exe文件,以便独立运行它。这两者仅适用于Windows,而Java打包工具会在各个平台(Windows、macOS、Linux)上创建可执行文件,当在相应平台上运行时(它不支持跨平台,例如无法在Windows上创建macOS可执行文件)。

对于macOS,有一个名为appbundler的工具,它能够创建一个带有JRE和jar文件的.app文件,因此用户只需双击该应用程序即可启动Java应用程序。

英文:

Java 14 includes a packaging tool that allows exactly that. There are also tutorials available on how to do that, e.g. here. In short, something like this should work:

jpackage --input target/ \
  --name MyApp \
  --main-jar myApp.jar \
  --main-class com.company.MyApp \
  --type exe \
  --java-options '--enable-preview'

If you are not yet using Java 14 but an older version, have a look at WinRun4J or Launch4J. Both allow creating exe-wrappers for Jar-Files, or creating an exe-file along with a JRE and a jar-File to run it independently. Those two only work for Windows, whereas the Java packaging tool will create executables for each platform (Windows, macOS, Linux) when run on the respective platform (it's not cross-platform capable, e.g. creating a macOS executable on Windows).

For macOS, there is appbundler which is able to create a .app with JRE and jar-File included, so users can just double click the application to start the java application.

huangapple
  • 本文由 发表于 2020年8月25日 23:02:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63581722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定