我应该如何将 Java 与我的 Windows 应用程序捆绑在一起?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6003816/
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
How should I bundle Java with my Windows application?
提问by aknuds1
I have a Windows application based on Java, that I should like to install with Java bundled. The installation framework is NSIS. The application executable should be guaranteed to invoke the bundled Java, so there's no clash with other Javas installed in the system.
我有一个基于 Java 的 Windows 应用程序,我想安装它并捆绑 Java。安装框架是NSIS。应保证应用程序可执行文件调用捆绑的 Java,因此不会与系统中安装的其他 Java 发生冲突。
What's the best way to achieve my goal? I haven't tried to solve this kind of problem before, and have little experience with Java, so I don't know which solutions are out there. I think I'd prefer Java to be embedded in the application's executable, if feasible, otherwise I guess Java could be installed along with it (with the executable pointing to said Java).
实现我的目标的最佳方式是什么?这类问题我以前没有尝试过,对Java的经验也很少,所以不知道有哪些解决方案。如果可行,我想我更喜欢将 Java 嵌入到应用程序的可执行文件中,否则我猜 Java 可以与它一起安装(可执行文件指向所说的 Java)。
Edit:This project already generates an executable (.exe), via NSIS. The executable will by default use the system Java, but apparently it'll prefer a JRE in the same directory (i.e. bundled) if present.
编辑:该项目已经通过 NSIS 生成了一个可执行文件 (.exe)。默认情况下,可执行文件将使用系统 Java,但显然它更喜欢同一目录(即捆绑)中的 JRE(如果存在)。
Edit 2: The scenario that made me want to bundle Java with this application was that I received an executable for it built with 32-bit Java, which failed (silently) on my system which has 64-bit Java.
编辑 2:让我想将 Java 与此应用程序捆绑在一起的情况是,我收到了一个用 32 位 Java 构建的可执行文件,但在我的具有 64 位 Java 的系统上(无声地)失败了。
采纳答案by leonbloy
Are you absolutely sure you don't want to use the computer JRE? In most cases it's preferable. You can see here(and the included link) some examples with installers that check JRE number and install it (globally) if necessary.
您绝对确定不想使用计算机 JRE 吗?在大多数情况下,它是可取的。您可以在此处(以及包含的链接)查看一些带有安装程序的示例,这些安装程序检查 JRE 编号并在必要时(全局)安装它。
If you really prefer to include your own JRE in the installer and always use it - what prevents you from doing it? It's just a matter of your main program point having some way of konwing the JRE location and forcing to use it. That depends on how you pack/invoke your Java program. Normally, it would be feasible, perhaps with a simple .bat file - perhaps to be created at installation time.
如果您真的更喜欢在安装程序中包含您自己的 JRE 并始终使用它 - 是什么阻止您这样做?这只是您的主程序点具有某种方式来控制 JRE 位置并强制使用它的问题。这取决于您如何打包/调用 Java 程序。通常,它是可行的,也许使用一个简单的 .bat 文件 - 也许在安装时创建。
回答by MTilsted
The solution we used(A few years ago, but still I think the best way).
我们使用的解决方案(几年前,但我仍然认为是最好的方法)。
Find a program which can generate an exe file from a jar file(Google is your friend, I can't remember the name). Note that this .exe file will still need a jre. It is just a smart way to make an exe which contain your .jar file, and which start the java process automatic. (This also allows you to set a custom icon on your .exe file).
找一个可以从jar文件生成exe文件的程序(谷歌是你的朋友,我不记得名字了)。请注意,此 .exe 文件仍需要 jre。这只是一种制作包含 .jar 文件并自动启动 java 进程的 exe 的聪明方法。(这也允许您在 .exe 文件上设置自定义图标)。
The java sdk which you use to develop/compile your java application, also contains a folder called jre, which contain a copy of the jre.
用于开发/编译 java 应用程序的 java sdk 还包含一个名为 jre 的文件夹,其中包含 jre 的副本。
Include this folder with the installer, so the jre folder is located in the same folder as the .exe file. Then the .exe file will use the included jre, and the jre will not be installed on the computer, so you prevent any problems.
在安装程序中包含此文件夹,因此 jre 文件夹与 .exe 文件位于同一文件夹中。然后.exe文件会使用包含的jre,jre不会安装到电脑上,这样就可以防止出现任何问题。
回答by Voo
Well one extremely simple solution that works actually quite nice if you don't have to get an executable, is just using a simple Windows Batch file that starts the jar and having a shortcut to it so you get your preferred icon on it. For the average user there's no real difference - after all the default is to suppress known extensions on Windows (horrible default but well) - and for people who do know what an exe is, the solution should be quite apparent anyways.
好吧,如果您不必获得可执行文件,那么一个非常简单的解决方案实际上非常好用,就是使用一个简单的 Windows 批处理文件来启动 jar 并有一个指向它的快捷方式,这样您就可以获得您喜欢的图标。对于普通用户来说,没有真正的区别 - 毕竟默认是禁止 Windows 上的已知扩展(可怕的默认但很好) - 对于知道 exe 是什么的人来说,无论如何解决方案应该很明显。
That way you can also easily start the included java or better yet offer both versions and just change a variable in an ini file, which really is much nicer - nobody wants or needs X different JRE versions that are outdated and are nothing more than security problems. Still can't harm to offer a version that includes the JRE for people without a java install to make it as simple as possible for them.
这样,您还可以轻松启动包含的 java 或更好但提供两个版本,只需更改 ini 文件中的变量,这确实要好得多 - 没有人想要或需要 X 不同的过时的 JRE 版本,只不过是安全问题. 为没有安装 java 的人提供一个包含 JRE 的版本仍然没有坏处,以使其尽可能简单。
One example for this behavior would be weka..
这种行为的一个例子是weka..