Java: Get application path, in IDE and JAR file

Veröffentlicht von

How to get the application path in a Java application.

Let’s have look at different approaches, how to get the application path in a Java application. There are different approaches and which one is the right for you depends on your application scenario. In my case, I want to access some files in the application folder. This should work in the IDE and also for the JAR file.

My demo application looks like that:

new File(".").getCanonicalPath();

This is an often proposed solution for the problem. The solution usually works, however we are not getting the application path here, instead this returns the working folder.

String path = new File(".").getCanonicalPath();

If we run this in the IDE, we get the path to the root of the project, which is fine.

C:\_git\Learning.git\multimaven

The output from the JAR gives us also the path where the JAR file resides:

C:\_git\Learning.git\multimaven\Application\target

The solution breaks if we run our application from the different path:

C:\_git\Learning.git\multimaven\Application

As you can see, we call the JAR file from a folder below and this returns this folder. This behavior makes this function not very reliable for my use cases.


System.getProperty("user.dir")

This approach is also regularly mentioned, but there is no difference to the approach used before. It returns the working folder.

C:\_git\Learning.git\multimaven

new File(Main.class.getProtectionDomain() …

Another solution. This will get us the path to the current class directory, however this returns us different paths for running the in IDE and from the JAR file.

var path = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath();

If we run the code in the IDE, we get the „classes“ folder returned, where the IDE puts the compiled versions of our classes.

C:\_git\Learning.git\multimaven\Application\target\classes

If we package our application in a JAR file, we get the folder and filename for the JAR file.

C:\_git\Learning.git\multimaven\Application\target\Application-1.0-SNAPSHOT-shaded.jar


ProgDirUtil.getProgramDirectory()

After some searching the internet, I found this post on StackOverflow.

The approach uses a utility class:

var path = ProgDirUtil.getProgramDirectory();

The class:

public class ProgDirUtil {
    private static String getJarName() {
        return new File(ProgDirUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getName();
    }

    private static boolean runningFromJAR() {
        String jarName = getJarName();
        return jarName.contains(".jar");
    }

    public static String getProgramDirectory()
    {
        if (runningFromJAR()) {
            return getCurrentJARDirectory();
        } else {
            return getCurrentProjectDirectory();
        }
    }

    private static String getCurrentProjectDirectory() {
        return new File("").getAbsolutePath();
    }

    private static String getCurrentJARDirectory() {
        try {
            return new File(ProgDirUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParent();
        } catch (URISyntaxException exception) {
            exception.printStackTrace();
        }
        return null;
    }
}

The class checks if our code is running from the JAR file or from class files, like in the IDE. This returns the same path for both our scenarios, and also returns the correct path if we run the JAR file from a different working directory.

C:\_git\Learning.git\multimaven

Conclusion

What solution is best really depends on your requirements, but in the end the utility class seems to offer the most reliable way to get the application path, as it works consistent in the development environment and also when running the JAR file.

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht.