Double Clicking JAR file does not open Command Prompt
Asked Answered
O

3

6

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?

Oxide answered 4/5, 2012 at 10:23 Comment(3)
When you associate your .jar to javaw it deliberately does not show a command window!! associate it to java, and you should see the windowItu
@Petesh Can you add this comment as an answer? I'm sure you'll be rewarded.Insomnolence
I did associate with java.exe. However, the window is only shown for a fraction of second. Kindly refer the updated postOxide
I
12

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)

Itu answered 4/5, 2012 at 10:35 Comment(7)
I associated jar to java.exe present at jre/bin. However, this does opens a command prompt for a second and vanishes off. I am expecting a user input using BufferedReader. So it should wait for some user input before just vanishing off like that.Oxide
if you used the simple 'Open With...' option then the -jar option would be missing, causing the problem you're encountering. I'll update the answer with a set of options that can be used from the command line to configure this properlyItu
Thanks for updating the answer. I followed the solution setting both, jarfileterm and jarfile. Associated .jar to jarfileterm. However, this solution does not work :( The same problem still persists. Command Prompt is shown only for a fraction of second.Oxide
When you copy what is the equivalent of invoking the jarfileterm action into a terminal window: "c:\program files\java\jre7\bin\java.exe" -jar "c:\users\my account\documents\myjar.jar" does it run correctly?Itu
do ftype jarfileterm=cmd /s /k ""C:\Program Files\Java\jre7\bin\java.exe" -jar "%1" %*" followed by assoc .jar=jarfileterm followed by double-clicking the jar file - if there are any errors from the invocation they will be kept in the terminal windowItu
Did it. But it makes no change.Oxide
I'm going to update the answer with more detail on tracking down the issue - if, after you made the cmd /s /k ftype change, you should always see a cmd window when you double click on jar file, and it should remain after the command has completed. If you encountered no error when issuing the ftype and assoc then the change should have been reflected in the response to the double clickItu
S
1

May be your .jar file has binded with any other default program, Right click and open with 'Java(TM) Platform SE binary'. This should work if have a executable jar file.

Stentorian answered 4/5, 2012 at 10:33 Comment(0)
W
0

I am using the JDK to open the jar file in Windows 10.

  1. Open regedit → HKEY_CLASSES_ROOT\jarfile\shell\open\command
  2. Change the default from javaw to java

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

Whitlow answered 11/8, 2020 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.