Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a .jar file in a command prompt from double click

I'll start of by saying Im on windows 7.

I have created a .jar file which executes fine from the command line using the - java -jar myJar.jar approach

But what I'm after is to be able to double click on the jar file and for it to open up the command prompt window and run in the command prompt as if i've just typed the java -jar myJar.jar line.

When I double click the jar file I do think it is running because a visual part of the java is appearing, but there is no command prompt window showing my console output.

After looking around I've come across people saying that javaw which is what the jar files are associated with don't have a console and that I need to associate jar files with java.exe instead of javaw.exe. I've tried this and it didn't seem to work.

Can anyone help? A step by step would be nice.

like image 887
Nexus490 Avatar asked May 14 '13 12:05

Nexus490


4 Answers

I had the same question and the bat file idea was genius and saved me a lot of time rewriting code. Thanks!(I would have upvoted,but apparently I don't have enough rep.)

Batch (or Bat) files are super easy to make.

Just put the java -jar YourFile.jar into notepad (if you're on windows), save as Title.bat, and put into the same folder as your jar.

presto! a program open-able by the general public.

like image 80
user2439749 Avatar answered Sep 27 '22 21:09

user2439749


This is IMHO not possible. You could open the console from the application itself, but that is OS-dependent. If you need the console open, you have to run the application from it as you already do.

like image 42
Jan Krakora Avatar answered Sep 27 '22 20:09

Jan Krakora


If you want to display the command line you have to launch your jar with the command line.

java -jar MyJar.jar

like image 32
Julien Bodin Avatar answered Sep 27 '22 19:09

Julien Bodin


I would do something like this:

(Tested in Widows XP with JRE 1.6, to support other OSs you should verify the path for each OS and select the appropriate console emulator (xterm, gnome-terminal... (check for existance and preference...)))

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath().substring(1);//Adds extra slash (??) didn't know why
        String decodedPath = URLDecoder.decode(path, "UTF-8");
        System.out.println(decodedPath);
        Runtime.getRuntime().exec("cmd /c start java -jar \"" + decodedPath + "\" actual_run");
    }
    else {
        System.out.println("Hello World");
        JOptionPane.showMessageDialog(null, "Hello World");
        System.in.read();
    }
}
like image 33
Ahmed KRAIEM Avatar answered Sep 27 '22 20:09

Ahmed KRAIEM