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

为君一笑 提交于 2019-11-29 14:05:26
Behnil

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.

user2439749

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.

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

java -jar MyJar.jar

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

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)

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)

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!