简体   繁体   English

双击 JAR 文件不打开命令提示符

[英]Double Clicking JAR file does not open Command Prompt

I want to run a Jar file by double clicking it.我想通过双击运行 Jar 文件。

Following is the only Java class file present in it.以下是其中存在的唯一 Java 类文件。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Sysout{
public static void main(String[] args) throws IOException{
    System.out.println("Hello World!");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String msg = br.readLine();
    System.out.println(msg);
    br.read();
}
}

And Manifest file has Main-Class defined.并且 Manifest 文件定义了 Main-Class。

Using this link, I successfully ran the Jar file by double-clicking the batch file.使用链接,我通过双击批处理文件成功运行了 Jar 文件。 This opens the command prompt and runs the main class defined.这将打开命令提示符并运行定义的主类。

However, if I double click the Jar file directly, nothing happens.但是,如果我直接双击 Jar 文件,则没有任何反应。 I also checked this link and associated my .jar to javaw.exe This link also suggests the same.我还检查了链接并将我的 .jar 与 javaw.exe 相关联链接也建议相同。 Also tried by associating the .jar with java.exe还尝试通过将 .jar 与 java.exe 相关联

What happens is the command prompt opens for a fraction of second and vanishes off.发生的情况是命令提示符打开几分之一秒然后消失。

Even if I am expecting the user to enter some data, double-clicking operation does not wait for the user to enter anything.即使我期待用户输入一些数据,双击操作也不会等待用户输入任何内容。

Where is the problem?问题出在哪儿?

When you use the javaw association, it does not create a command window, and swallows all the System.out and System.err invocations.当您使用javaw关联时,它不会创建命令窗口,并且会吞下所有System.outSystem.err调用。

You should reassociate your .jar file with the java binary, which should display the requisite command window.您应该将.jar文件与java二进制文件重新关联,这将显示必要的命令窗口。

If you used the simple Open With... option, it will have omitted the -jar option from the command line.如果您使用简单的Open With...选项,它将在命令行中省略-jar选项。

Open up an administrator command window (this is needed if you're using Vista or Windows 7 with UAC enabled) and do:打开管理员命令窗口(如果您使用的是启用了 UAC 的 Vista 或 Windows 7,则需要此窗口)并执行以下操作:

assoc .jar=jarfileterm
ftype jarfileterm="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*

In your case, you should replace the C:\\Program Files\\Java\\jre7\\bin\\java.exe path with the one for your install of the jre.在您的情况下,您应该将C:\\Program Files\\Java\\jre7\\bin\\java.exe路径替换为用于安装 jre 的路径。

When you double-click following this, then it should run correctly.当您按照此双击时,它应该可以正确运行。

You can add another ftype:您可以添加另一个 ftype:

ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*

again substituting the path to the javaw binary with the one that's for your system.再次用适用于您系统的路径替换 javaw 二进制文件的路径。

You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm and assoc .jar=jarfile您现在应该能够通过交替选择assoc .jar=jarfiletermassoc .jar=jarfile在窗口和非窗口之间切换

If you want to keep the command window around after running the .jar, then you surround the calling of the java command with a cmd /s /k viz:如果您想在运行 .jar 后保留命令窗口,那么您可以使用cmd /s /k即包围 java 命令的调用:

ftype jarfileterm=cmd /s /k ""C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*"
assoc .jar=jarfileterm

If these commands worked, then double clicking on the jar file will cause a command window to pop-up and persist.如果这些命令有效,则双击 jar 文件将导致弹出一个命令窗口并持续存在。

You cannot set a complex enough command line with either Open With... or using Default Programs that will allow the jar file to run.您不能使用Open With...或使用允许 jar 文件运行的Default Programs设置足够复杂的命令行。 If you have successfully tried all these efforts ftype and assoc commands and it still doesn't work, then you will need to peel out the registry editor.如果你已经成功地尝试了所有这些ftypeassoc命令,但它仍然不起作用,那么你需要剥离注册表编辑器。

Launch regedit , and search for a key called .jar under HKEY_CLASSES_ROOT - this should result in a single value underneath it called (Default) with a value, if your ftype command invocations worked, then it should read jarfileterm .启动regedit ,并在HKEY_CLASSES_ROOT下搜索一个名为 .jar 的键 - 这应该会在它下面生成一个名为(Default)的值,如果您的 ftype 命令调用有效,那么它应该读取jarfileterm If it didn't work, then you're looking at an association that may have been created by another application (I don't know if the java updater replaces these entries, but if it does, then this could be the issue)如果它不起作用,那么您正在查看可能已由另一个应用程序创建的关联(我不知道 java 更新程序是否替换了这些条目,但如果确实如此,那么这可能是问题所在)

You need to next look for this key in the HKEY_CLASSES_ROOT .接下来您需要在HKEY_CLASSES_ROOT查找此密钥。 It will find this entry, which should contain the a key Shell (ie expand the folder jarfileterm and it should reveal another folder Shell ), which contains a key Open which contains a key Command which contains a (Default) value that should contain the invocation command for launching .jar files.它将找到这个条目,它应该包含一个键Shell (即展开文件夹jarfileterm并且它应该显示另一个文件夹Shell ),其中包含一个键Open ,其中包含一个键Command ,其中包含一个(Default)值,应该包含调用用于启动 .jar 文件的命令。 This command should match the last ftype jarfileterm=... entries that you typed in. If it doesn't then you should make it match one of the cmd /s /k or "c:\\program files\\java\\jre7\\bin\\java.exe" options (depending on if you want to persist the command window in the event of an error in launching or not)此命令应匹配您输入的最后一个ftype jarfileterm=...条目。如果不匹配,则应使其匹配cmd /s /k或 "c:\\program files\\java\\jre7\\bin \\java.exe”选项(取决于您是否希望在启动时出现错误时保留命令窗口)

May be your .jar file has binded with any other default program, Right click and open with 'Java(TM) Platform SE binary'.可能是您的 .jar 文件已与任何其他默认程序绑定,右键单击并使用“Java(TM) Platform SE 二进制文件”打开。 This should work if have a executable jar file.如果有一个可执行的 jar 文件,这应该可以工作。

I am using the JDK to open the jar file in Windows 10.我正在使用 JDK 在 Windows 10 中打开jar文件。

  1. Open regedit → HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command打开regedit → HKEY_CLASSES_ROOT\\jarfile\\shell\\open\\command
  2. Change the default from javaw to java将默认值从javaw更改为java

For example, mine is "C:\\Program Files\\Java\\jdk-14.0.1\\bin\\java.exe" "-jar" "%1"例如,我的是"C:\\Program Files\\Java\\jdk-14.0.1\\bin\\java.exe" "-jar" "%1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM