I know this question has been asked a lot, but none of the previous answers seem to work for me.
If I make my app an executable Jar, is there any way of telling where that Jar is located on the file system when it is executed? My app needs to know, so it can extract a folder from within it's jar (by treating it as a zip file) to a specified directory.
EDIT: Thanks to everyone who contributed, but I think this is a dead end. I'll have to try something else. Sorry for wasting everyone's time. I'll keep checking back on this question, so if anybody does crack it, please answer :D
For what it's worth, here is my test code:
package jarProblem;
import java.io.File;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class Main {
public Main () {
ClassLoader loader = Main.class.getClassLoader();
String path = loader.getResource("jarProblem/Main.class").getPath();
File file = new File(path);
String pathToJar = file.getParentFile().getAbsolutePath();
JOptionPane.showMessageDialog(new JFrame(), new JLabel(pathToJar),"CurrentFolder", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
public static void main (String[] args) {
new Main();
}
}