9

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.

Nexus490
  • 319
  • 1
  • 4
  • 14
  • I think Microsoft people are trying to tell how to resolve your problem, follow this link: http://answers.microsoft.com/en-us/windows/forum/windows_7-windows_programs/since-updating-to-windows-7-i-am-unable-to-run-any/b4b2c2fb-8634-4d26-bf76-a27cb7e6cbff Hope it helps! – Juned Ahsan May 14 '13 at 12:34

8 Answers8

10

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.

Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
user2439749
  • 101
  • 2
2

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.

Community
  • 1
  • 1
Jan Krakora
  • 2,500
  • 3
  • 25
  • 52
1

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

java -jar MyJar.jar

Julien Bodin
  • 783
  • 3
  • 19
1

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();
    }
}
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
1

Alternatively I suggest creating a bat file with this content :

java -jar yourjar.jar

This will launch your jar as well as open the command prompt automatically, all from a simple double click on a bat file. (The bat file needs to be in the same folder as your jar file, else you need to specify the path to the jar, not just the jar name)

1

This is the easiest solution for beginners:

  1. Open any text editor
  2. write this two lines:

      java "yourmainclassname"
      pause
    
  3. save that file as "name".bat

  4. Run it with double click from windows GUI

(of course this new created .bat file must be in the same folder as the .class)

lewis4u
  • 14,256
  • 18
  • 107
  • 148
0

..but there is no command prompt window showing my console output.

No there wouldn't be a console for an executable Jar. You'll need to put that output in the GUI.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • So there is no way at all where I could get a jar file to open in the command prompt window from a double click? – Nexus490 May 14 '13 at 12:38
  • If you change *"open in the command prompt window"* to *"open with an associated command prompt window for output"* the answer is 'no'. I will not try to answer the question as you put it to me, since I cannot make sense of it. – Andrew Thompson May 14 '13 at 12:41
  • Well you've answered my question whether you meant to or not; Thank you. – Nexus490 May 14 '13 at 12:44
0

Check your MANIFEST.MF

  1. Extract your "executable" jar file into a folder
  2. find MANIFEST.MF in META-INF folder
  3. check presence of this field: Main-Class: YourMainClassHere

If this field dissapeared then open your original MANIFEST.txt for this point: Main-Class: YourMainClassHere must end with a new line or carriage return

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.