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

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

问题描述

我想通过双击来运行Jar文件。



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

  import java.io.BufferedReader; 
import java.io.InputStreamReader;
import java.io.IOException;
public class Sysout {
public static void main(String [] args)抛出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。



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



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



如果命令提示符打开几分之一秒就会消失。



即使我希望用户输入一些数据,双击操作也不会等待用户输入任何内容。



问题出在哪里?

解决方案

当你使用 javaw 关联,它不会创建一个命令窗口,并吞下所有 System.out System.err 调用。 / p>

您应该使用 java 重新关联 .jar 文件二进制文件,它应显示必需的命令窗口。



如果使用简单的打开方式... 选项,它将从命令行中省略 -jar 选项。



打开管理员命令窗口(这是如果您正在使用启用了UAC的Vista或Windows 7,则需要这样做:

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

在您的情况下,你应该用你安装的jre替换 C:\Program Files \Java \ jre7 \ bin\java.exe 路径。



当您双击此后,它应该正确运行。



您可以添加另一个ftype:

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

再次使用适合您系统的路径替换javaw二进制文件的路径。



您现在应该可以通过交替选择 assoc .jar = jarfileterm 来切换窗口窗口和非窗口窗口。 assoc .jar = jarfile



如果你想在运行.jar之后保持命令窗口,那么y你用 cmd / s / k 来调用java命令:viz:

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

如果这些命令有效,那么双击jar文件将导致命令窗口弹出并保持。



您无法使用 Open With ... 或者设置足够复杂的命令行使用默认程序,它将允许运行jar文件。如果你已经成功地尝试了所有这些努力 ftype assoc 命令,它仍然不起作用,那么你将需要剥离注册表编辑器。



启动 regedit ,并在<$ c下搜索名为.jar的密钥$ c> HKEY_CLASSES_ROOT - 如果你的ftype命令调用有效,这应该在它下面产生一个名为(默认)的值,如果你的ftype命令调用工作,那么它应该是 jarfileterm 。如果它不起作用,那么你正在查看一个可能由另一个应用程序创建的关联(我不知道java updater是否替换了这些条目,但如果确实如此,那么这可能是问题)



您需要在 HKEY_CLASSES_ROOT 中查找此密钥。它会找到这个条目,它应该包含一个键 Shell (即展开文件夹 jarfileterm ,它应该显示另一个文件夹 Shell ),其中包含一个键打开,其中包含一个键 Command ,其中包含(默认)值,该值应包含用于启动.jar文件的调用命令。此命令应与您输入的最后一个 ftype jarfileterm = ... 条目相匹配。如果没有,则应使其与 cmd / s / k 或c:\program files\java\jre7\bin\java.exe选项(取决于您是否要在命令窗口中保留命令窗口)启动时是否发生错误)


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

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

And Manifest file has Main-Class defined.

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.

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.

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.

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

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

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

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

You can add another 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.

You should now be able to toggle between windowed and non-windowed by alternately choosing assoc .jar=jarfileterm and assoc .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:

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.

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.

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)

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

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

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