双击 JAR 文件不打开命令提示符 [英] Double Clicking JAR file does not open Command Prompt

查看:20
本文介绍了双击 JAR 文件不打开命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过双击运行 Jar 文件.

I want to run a Jar file by double clicking it.

以下是其中存在的唯一 Java 类文件.

Following is the only Java class file present in it.

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();
}
}

Manifest 文件定义了 Main-Class.

And Manifest file has Main-Class defined.

使用 this 链接,我成功运行了 Jar 文件双击批处理文件.这将打开命令提示符并运行定义的主类.

Using this link, I successfully ran the Jar file by double-clicking the batch file. This opens the command prompt and runs the main class defined.

但是,如果我直接双击 Jar 文件,则没有任何反应.我还检查了这个将我的 .jar 链接并关联到 javaw.exe链接也建议相同.还尝试通过将 .jar 与 java.exe 相关联

However, if I double click the Jar file directly, nothing happens. I also checked this link and associated my .jar to javaw.exe This link also suggests the same. Also tried by associating the .jar with 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.

问题出在哪里?

推荐答案

当你使用javaw关联时,它不会创建命令窗口,并吞下所有的System.outSystem.err 调用.

When you use the javaw association, it does not create a command window, and swallows all the System.out and System.err invocations.

您应该将 .jar 文件与 java 二进制文件重新关联,这应该会显示必要的命令窗口.

You should reassociate your .jar file with the java binary, which should display the requisite command window.

如果您使用简单的 Open With... 选项,它将在命令行中省略 -jar 选项.

If you used the simple Open With... option, it will have omitted the -jar option from the command line.

打开管理员命令窗口(如果您使用的是启用了 UAC 的 Vista 或 Windows 7,则需要此窗口)并执行:

Open up an administrator command window (this is needed if you're using Vista or Windows 7 with UAC enabled) and do:

assoc .jar=jarfileterm
ftype jarfileterm="C:Program FilesJavajre7injava.exe" -jar "%1" %*

在您的情况下,您应该将 C:Program FilesJavajre7injava.exe 路径替换为用于安装 jre 的路径.

In your case, you should replace the C:Program FilesJavajre7injava.exe path with the one for your install of the jre.

当您双击以下内容时,它应该可以正常运行.

When you double-click following this, then it should run correctly.

您可以添加另一个 ftype:

You can add another ftype:

ftype jarfile="C:Program FilesJavajre7injavaw.exe" -jar "%1" %*

再次用适用于您系统的路径替换 javaw 二进制文件的路径.

again substituting the path to the javaw binary with the one that's for your system.

您现在应该可以通过交替选择 assoc .jar=jarfiletermassoc .jar=jarfile

You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm and assoc .jar=jarfile

如果你想在运行 .jar 后保留命令窗口,那么你可以用 cmd/s/k 包围 java 命令的调用,即:

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:

ftype jarfileterm=cmd /s /k ""C:Program FilesJavajre7injava.exe" -jar "%1" %*"
assoc .jar=jarfileterm

如果这些命令有效,则双击 jar 文件将导致弹出命令窗口并持续存在.

If these commands worked, then double clicking on the jar file will cause a command window to pop-up and persist.

您不能使用 Open With... 或使用 Default Programs 设置足够复杂的命令行,以允许 jar 文件运行.如果您已经成功尝试了所有这些ftypeassoc 命令,但仍然无法正常工作,那么您需要剥离注册表编辑器.

You cannot set a complex enough command line with either Open With... or using Default Programs that will allow the jar file to run. 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.

启动 regedit,然后在 HKEY_CLASSES_ROOT 下搜索一个名为 .jar 的键 - 这应该会在它下面生成一个名为 (Default) 带有一个值,如果您的 ftype 命令调用有效,那么它应该读取 jarfileterm.如果它不起作用,那么您正在查看可能已由另一个应用程序创建的关联(我不知道 java 更新程序是否替换了这些条目,但如果确实如此,那么这可能是问题所在)

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. 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)

接下来您需要在 HKEY_CLASSES_ROOT 中查找此密钥.它将找到这个条目,它应该包含一个键 Shell(即展开文件夹 jarfileterm 并且它应该显示另一个文件夹 Shell),它包含一个键 Open,其中包含一个键 Command,其中包含一个 (默认) 值,该值应包含用于启动 .jar 文件的调用命令.此命令应匹配您输入的最后一个 ftype jarfileterm=... 条目.如果不匹配,则应使其与 cmd/s/k 或c:program filesjavajre7injava.exe"选项(取决于您是否希望在启动时出现错误时保留命令窗口)

You need to next look for this key in the HKEY_CLASSES_ROOT. It will find this entry, which should contain the a key Shell (i.e. 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. 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 filesjavajre7injava.exe" options (depending on if you want to persist the command window in the event of an error in launching or not)

这篇关于双击 JAR 文件不打开命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆