0

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

}
CptSpike
  • 33
  • 8

2 Answers2

0
System.getProperty("user.dir");
Gabriel Câmara
  • 1,249
  • 15
  • 22
  • Returns "C:/Windows/System32" which is where javaw.exe is located – CptSpike Mar 04 '13 at 13:36
  • Your javaw is located in System32 folder? From what I know, javaw is installed, by default, in Program Files/Java/jdk/bin... Maybe your environment variables are returning this path. – Gabriel Câmara Mar 04 '13 at 13:48
  • I thought that was odd. My JDK is in Program Files/Java and does have a javaw.exe too. Either way, it's not pointing to my desktop :( – CptSpike Mar 04 '13 at 13:53
  • Take a look at your environment variables and the paths there... seems to be bad configuration – Gabriel Câmara Mar 04 '13 at 13:56
  • Seems to be nothing out of the ordinary to me. The C:/Windows/System32.javaw.exe isn't mentioned at all, funnily – CptSpike Mar 04 '13 at 13:59
  • That's because We don't put the absolute path with filename (i.e. C:/Windows/System32/javaw.exe). We put the absolute path and nothing else... But what I mean is that your "USER DIR" or "PATH" may be returning the system32 path, this way, the java or javaw may not identify which is your correct path. – Gabriel Câmara Mar 04 '13 at 14:06
0

Have you tried the Path class? It has everything that your looking for.

user1230731
  • 55
  • 1
  • 6