你需要安装java来运行jar文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20434558/
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-08-13 01:53:41  来源:igfitidea点击:

Do you need java installed to run jar files?

javaexecutable-jar

提问by user3059427

I want to create a executable jar file of a small game that i wrote in java and make it playable in any machine with simple double click like exe file. My question is do you need to install java runtime first in order for executable jar file to work or can it work on a machine without any java installed as well?

我想创建一个我用java编写的小游戏的可执行jar文件,并像exe文件一样通过简单的双击使其可以在任何机器上播放。我的问题是您是否需要先安装 java 运行时才能使可执行 jar 文件工作,还是可以在没有安装任何 java 的机器上工作?

采纳答案by SiN

No it can't. However, clients do not require the JDK, the JRE would do. You can make your jar declare its own main class and have users double click it normally like an exe file or create a bat file in Windows.

不,不能。但是,客户端不需要 JDK,JRE 可以。你可以让你的 jar 声明它自己的主类,并让用户像 exe 文件一样双击它,或者在 Windows 中创建一个 bat 文件。

回答by óscar López

Do you need to install java runtime first in order for executable jar file to work?

您是否需要先安装 java 运行时才能使可执行 jar 文件工作?

Yes, of course. To run Java .jar files first you need to have installed at leastthe JRE (run time environment). The JDK (development kit) is a superset of the JRE and will also work for running .jar files.

是的当然。要首先运行 Java .jar 文件,您至少需要安装JRE(运行时环境)。JDK(开发工具包)是 JRE 的超集,也适用于运行 .jar 文件。

Can it work on a machine without any java installed as well?

它可以在没有安装任何 java 的机器上工作吗?

No, as mentioned above, at the bare minimum the JRE must be installed.

不,如上所述,必须至少安装 JRE。

回答by Andrei Nicusan

It requires a JRE instance to be installed on the host machine. This is because JAR files are actually executed via a command line like (in Windows):

它需要在主机上安装 JRE 实例。这是因为 JAR 文件实际上是通过命令行执行的(在 Windows 中):

java -jar ...

This means that somewhere on your system the java.exe executable should be reachable, either by including its folder in the PATH (Windows) or replacing javawith its full path.

这意味着应该可以访问系统上的某个位置 java.exe 可执行文件,方法是将其文件夹包含在 PATH (Windows) 中或替换java为其完整路径。

Also, most likely you will need to have all the runtime Java libraries to be hosted on the system, as the JAR file containing the application you want to run is not supposed to contain all the Java API libraries. They are also part of the JRE package.

此外,您很可能需要在系统上托管所有运行时 Java 库,因为包含您要运行的应用程序的 JAR 文件不应该包含所有 Java API 库。它们也是 JRE 包的一部分。

My explanation is tied to Windows for the sake of examples, but it can be extended to any OS.

为了示例,我的解释与 Windows 相关联,但它可以扩展到任何操作系统。

回答by chrylis -cautiouslyoptimistic-

You have to have a Java runtime environment (JRE) available on the machine unlessyou use a tool that performs ahead-of-time compilation (AOT, which is contrast to the usual Just-In-Time). Such tools are available (such as Excelsior JET), but they have a number of downsides, including cost and the fact that a precompiled Java application is a regular executable and will only run on one operating system. I've seen some installers that will detect whether a JRE is installed and launch the Java installer for the user if not.

除非您使用执行提前编译的工具(AOT,这与通常的 Just-In-Time 形成对比),否则您必须在机器上有可用的 Java 运行时环境 (JRE )。此类工具是可用的(例如Excelsior JET),但它们有许多缺点,包括成本以及预编译的 Java 应用程序是常规可执行文件并且只能在一个操作系统上运行这一事实。我已经看到一些安装程序会检测是否安装了 JRE,如果没有,则为用户启动 Java 安装程序。

回答by Magimaidas

Yes ! of course, JRE is required and it is not compulsory for JDK to be installed. Since, the main class is defined in JRE for .jar files, it is necessary to have JRE on your machine. I tried with Windows OS.

是的 !当然,JRE是必须的,JDK不是强制安装的。由于主类是在 JRE 中为 .jar 文件定义的,因此必须在您的机器上安装 JRE。我尝试使用 Windows 操作系统。

回答by Bane2000

Actually you can bundle JRE within your exe file with several java .exe wrappers. Here are few of them JSmooth, Launch4j, Jar2Exe.

实际上,您可以将 JRE 与多个 java .exe 包装器捆绑在您的 exe 文件中。这里有一些 JSmooth、Launch4j、Jar2Exe。