6

I have a .jar file and when I run it from the command prompt via java -jar MyJar.jar, it works fine. However double-clicking on it doesn't. Double-clicking starts the program correctly, but something on the inside doesn't work.

For the purposes of trying to figure out what is wrong on my own: what is the difference between double-clicking on a runnable .jar vs running it from the command line?

melvio
  • 762
  • 6
  • 24
Collin
  • 306
  • 3
  • 8
  • Double click launches javaw not java, see: http://stackoverflow.com/questions/1997718/difference-between-java-exe-and-javaw-exe – morpheus05 Jul 11 '13 at 21:42
  • What do you mean by "something on the inside doesn't work"? – XiaoChuan Yu Jul 11 '13 at 21:42
  • 1
    The main difference between the two is the context of the execution location. When running from the command line, the execution context will be the directory you executed the java command from. When double clicking the Jar the context will be system dependent. You can use System.out.println(new File(".").getCanonicalPath()); to output the location that the program was executed in – MadProgrammer Jul 11 '13 at 21:44
  • It launches correctly, but the program itself doesn't operate correctly. So the GUI shows up, but the program doesn't work. – Collin Jul 11 '13 at 21:45
  • @morpheus05 Not always true, Java doesn't just work on Windows! – MadProgrammer Jul 11 '13 at 21:46
  • Ok, I got it fixed and as far as I can tell its a problem/bug with eclipse. I've been exporting my project as a runnable jar and double-clicking on it inside of the eclipse package explorer: that didn't work. So I double-click on it in the regular file browser and it works perfectly. Problem fixed but can anyone explain why this happens? EDIT: It "broke" in the same way on someone else's machine, so I don't think its just something weird about the machine that I'm using. – Collin Jul 11 '13 at 21:57
  • How many JDK, JREs are installed on your computer? My be the one which is associated with JAR files in windows explorer, is not the same which is in your windows path. – Amir Pashazadeh Jul 11 '13 at 22:23
  • Double clicking on a jar inside Eclipse, by default, does "open" which will have the system do whatever it thinks it does to "run" a jar. (On my machine, it runs WinZip.) The default action for Windows, to "run" a jar, is to execute it with `javaw -jar`. This is the same thing double clicking on the icon in the Windows Explorer does. It uses the same JDK since Eclipse defers to Windows. But if you select the jar, right click and chose some other "open" action, Eclipse will start doing that instead of letting the system "run" it. – Lee Meador Jul 12 '13 at 16:23

2 Answers2

0

Double clicking runs it as "javaw -jar MyJar.jar"

Lee Meador
  • 12,829
  • 2
  • 36
  • 42
  • Not always true, for example, on MacOS, there is no javaw command. And you've not even mentioned how this might effect the execution context of the application, at best, this would make a reasonable comment – MadProgrammer Jul 11 '13 at 21:46
  • @MadProgrammer True. But I think I guessed some unwritten parts of the question when assuming it was referring to Windows only. – Lee Meador Jul 11 '13 at 21:49
0

Where the program is executed is important and can change depending on how it was executed.

You can test this by using something like...

import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class WhereAmI {

    public static void main(String[] args) {
        new WhereAmI();
    }

    public WhereAmI() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                try {
                    String path = new File(".").getCanonicalPath();
                    JOptionPane.showMessageDialog(null, "I was started in " + path);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }

            }
        });
    }

}

For example. When compiled, the Jar resides in /Volumes/Disk02/DevWork/personal/java/projects/wip/StackOverflow/WhereAmI/dist

If I change directories to this location and run java -jar WhereAmI.jar it outputs

enter image description here

If I change directories to /Volumes/Disk02/DevWork/personal/java/projects/wip/StackOverflow/WhereAmI and run java -jar dist/WhereAmI.jar it outputs

enter image description here

The execution context has changed. The same thing will happen when you double click the Jar and it is system dependent. It will also matter if it's a short cut or the actual Jar.

This will mean that if you rely on any relative resources, you must make sure that the Jar is executed within the correct location, relative to your resources.

How to achieve this is dependent on the OS

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I'm curious as to the reason for the downvote? In what ways does this not answer the question been asked? In what ways could it be improved? – MadProgrammer Jan 26 '20 at 02:40