What is the default working directory for a jar launched from Windows Explorer?

Orange picture Orange · Nov 18, 2011 · Viewed 8.5k times · Source

I have created a jar file of my application so that users can run it from Windows Explorer. It loads a JSON file in its own working directory for some settings (using the path ".\config.json"). However, when I run it from the jar file from Windows Explorer, it cannot find the config file, while running the same jar from the command line works. I'm assuming this is because explorer sets the working directory to something other than the jar's folder. What does it set it to? (And how can I find the jar's own folder?)

Answer

user177800 picture user177800 · Nov 18, 2011

Why not try printing out System.getProperty("user.dir"); and finding out what the application thinks?

user.dir    User's current working directory

use the following to get the PARENT of where you .jar is loading from, which is what it sounds like you want

import java.io.*;

public class Main
{
    public static void main(final String[] args) throws IOException
    {
        final File file = new File(".");
        System.out.println("file = " + file.getAbsoluteFile().getParent());
    }
}

The output when I run it from the command line or double clicking from Windows Explorer is:

file = c:\Users\Jarrod Roberson\Projects\SwingJarFinder