Java 如何打开/运行 .jar 文件(双击不起作用)?

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

How to open/run .jar file (double-click not working)?

javawindowswindows-8jar

提问by nivk

I can't open or run my .jar file.

我无法打开或运行我的 .jar 文件。

I just installed java, but I tried to open the .jar with other programs first, so the double-click defaults to something else and I can't change it back.

我刚刚安装了java,但我尝试先用其他程序打开.jar,所以双击默认为其他内容,我无法将其更改回来。

java -jar myfile.jar`

Above command returns:

以上命令返回:

'java' is not recognized as an internal or external command, operable program or batch file.

'java' 不是内部或外部命令,也不是可运行的程序或批处理文件。

Is there a way I can still open/run this?

有没有办法我仍然可以打开/运行它?

e: OS is Windows 8.

e: 操作系统是 Windows 8。

Also, I downloaded the .jar file; didn't create it myself (if that's relevant) Not sure if it contains an executable (but I think it does).

另外,我下载了 .jar 文件;没有自己创建它(如果相关)不确定它是否包含可执行文件(但我认为它包含)。

回答by Kevin

There are two different types of Java to download: The JDK, which is used to write Java programs, and the RE(runtime environment), which is used to actually run Java programs. Are you sure that you installed the RE instead of the SDK?

有两种不同类型的 Java 可供下载:用于编写 Java 程序的JDK和用于实际运行 Java 程序的RE(运行时环境)。你确定你安装的是 RE 而不是 SDK?

回答by Riggs

you can use the command prompt:

您可以使用命令提示符:

javaw.exe -jar yourfile.jar

Hope it works for you.

希望对你有效。

回答by Arjun

I downloaded the latest JDK 7u10. Once you do that, try running your jar, It should execute.

我下载了最新的 JDK 7u10。完成后,尝试运行您的 jar,它应该会执行。

回答by Filippp

Short trick: after I only REMOVED SPACES from names of the folders, where the .jar file was, double-clicked worked and the file executed.

小技巧:在我只从文件夹名称中删除空格后,.jar 文件所在的位置,双击工作并执行文件。

回答by Jonathan Caceres Romero

In cmd you can use the following:

在 cmd 中,您可以使用以下命令:

c:\your directory\your folder\build>java -jar yourFile.jar 

However, you need to create you .jar file on your project if you use Netbeans. How just go to Run ->Clean and Build Project(your project name)

但是,如果使用 Netbeans,则需要在项目中创建 .jar 文件。如何去运行 - >清理和构建项目(你的项目名称)

Also make sure you project properties Build->Packing has a yourFile.jar and check Build JAR after Compiling check Copy Depentent Libraries

还要确保您的项目属性 Build->Packing 有一个 yourFile.jar 并在编译后检查 Build JAR 检查 Copy Depentent Libraries

Warning: Make sure your Environmental variables for Java are properly set.

警告:确保您的 Java 环境变量设置正确。

Old way to compile and run a Java File from the command prompt (cmd)

从命令提示符 (cmd) 编译和运行 Java 文件的旧方法

Compiling: c:\>javac Myclass.java
Running: c:\>java com.myPackage.Myclass

I hope this info help.

我希望这些信息有帮助。

回答by Barbara Moore

I was having this same issue for both Windows 8 and Windows Server 2012 configurations. I had installed the latest version of JDK Java 7 and had set my **JAVA_HOME**system env variable to the jre folder: *C:\Program Files (x86)\Java\jre7* I also added the bin folder to my **Path**system env variable: *%JAVA_HOME%\bin*

对于 Windows 8 和 Windows Server 2012 配置,我都遇到了同样的问题。我已经安装了最新版本的 JDK Java 7 并将我的**JAVA_HOME**系统环境变量设置为 jre 文件夹:*C:\Program Files (x86)\Java\jre7* 我还将 bin 文件夹添加到我的**Path**系统环境变量中:*%JAVA_HOME%\bin*

But I was still having problems with double clicking the executable jar files. I found another system env variable OPENDS_JAVA_ARGSthat can be used to set the optional properties for javaw.exe. So I added this variable and set it to: -jar

但是我仍然在双击可执行 jar 文件时遇到问题。我发现了另一个系统环境变量OPENDS_JAVA_ARGS,可用于设置 javaw.exe 的可选属性。所以我添加了这个变量并将其设置为:-jar

Now I am able to run the executable jar files when double clicking them.

现在我可以在双击它们时运行可执行的 jar 文件。

回答by Thomaskutty Baby Eranakulam

In Netbeans please delete current you create the jar file and on the project explore of Netbeans please clean and build 2 or 3 times and right the project folder in project explore and build the jar file.

在 Netbeans 中,请删除当前创建的 jar 文件,在 Netbeans 的项目探索中,请清理并构建 2 或 3 次,然后在项目探索中正确放置项目文件夹并构建 jar 文件。

回答by Berk Soysal

An easy way to execute .jar files is to create a batch file.

执行 .jar 文件的一种简单方法是创建批处理文件。

Let's say you placed your jar file on your Desktop;

假设您将 jar 文件放在桌面上;

@echo OFF
java -jar C:\Users\YourName\Desktop\myjar.jar

Copy this code to a .txt file, modify "YourName" and save as "myjar.bat". Then whenever you double click, the jar file will be executed. Hope this helps.

将此代码复制到 .txt 文件中,修改“YourName”并另存为“myjar.bat”。然后每当您双击时,就会执行 jar 文件。希望这可以帮助。

回答by Shantanu Srivastava

Go to your java directory, Copy this path C:\Program Files\Java\jdk1.8.0_40\bin

转到您的 java 目录,复制此路径 C:\Program Files\Java\jdk1.8.0_40\bin

Right click on my computer , click properties, then go to "Advanced system settings" click , Environment variables. go to "System variables" table, find an entry named "path". Double click it and go to the end, put a semicolon and paste your path, apply and ok. It should run now.

右击我的电脑,点击属性,然后进入“高级系统设置”点击,环境变量。转到“系统变量”表,找到名为“路径”的条目。双击它并转到最后,放一个分号并粘贴您的路径,应用并确定。现在应该可以运行了。

回答by Teodor

I had this problem a while back and the solution was really easy.

不久前我遇到了这个问题,解决方案非常简单。

Just uninstall the current version of Java, download an older one, then uninstall the older and install the latest again.

只需卸载 的当前版本Java,下载旧版本,然后卸载旧版本并再次安装最新版本。

For example: Java 8 Update 73current install Java 7 Update 95.

例如:Java 8 Update 73当前安装 Java 7 Update 95。

How it works: Java's registry keys were messed up, and when you install the older version they get fixed.

工作原理:Java的注册表项被弄乱了,当您安装旧版本时,它们会得到修复。