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?
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
If I change directories to /Volumes/Disk02/DevWork/personal/java/projects/wip/StackOverflow/WhereAmI
and run java -jar dist/WhereAmI.jar
it outputs
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
Double clicking runs it as "javaw -jar MyJar.jar"