Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Clicking JAR file does not open Command Prompt

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?

like image 271
whitehat Avatar asked May 04 '12 10:05

whitehat


2 Answers

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)

like image 64
Petesh Avatar answered Oct 18 '22 03:10

Petesh


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.

like image 30
RP- Avatar answered Oct 18 '22 03:10

RP-