Determine current working directory in Java

July 07, 2020 No comments Java IO Java Current workdir

1. Introduction

In this article, we are going to present how to get the current working directory in Java.

Follow this link to check more Java IO related articles

2. Using system property user.dir

According to JDK 7 documentation current user directory is named by the system property user.dir, and is typically the directory in which the JVM was invoked.

Let's create a simple POC (proof of concept) application to check how this property behaves when we run the program from different folders.

This sample Maven application will be using maven-jar-plugin to build the executable jar file.

The pom.xml file has the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <artifactId>java-io-working-directory</artifactId>
    <packaging>jar</packaging>
    <groupId>com.frontbackend.java</groupId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>
                                com.frontbackend.java.io.working.dir.WorkingDirectory
                            </mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The WorkingDirectory is our base class that contains main(...) method, that starts the program:

package com.frontbackend.java.io.working.dir;

public class WorkingDirectory {

    public static void main(String[] args) {
        String userDirectory = System.getProperty("user.dir");
        System.out.println("Working dir = " + userDirectory);
    }
}

In pom.xml we need to point this class in configuration.archive.manifest.mainClass path:

<mainClass>
    com.frontbackend.java.io.working.dir.WorkingDirectory
</mainClass>

Now let's build our application using mvn install command:

We run this commend in /home/frontbackend/tutorials/java/java-io-working-directory folder:

$ mvn clean install
$ cd target
$ java -jar java-io-working-directory-1.0-SNAPSHOT.jar

The output will be:

Working dir = /home/frontbackend/tutorials/java/java-io-working-directory/target

After we copy application JAR file to /tmp folder:

$ cp java-io-working-directory-1.0-SNAPSHOT.jar /tmp
$ cd /tmp
$ java -jar java-io-working-directory-1.0-SNAPSHOT.jar

The output will be:

Working dir = /tmp

3. Get a working directory in Windows

Things are slightly different in the Windows operation system. Printing user.dir system parameter in Windows could give you results as: C:\WINDOWS\system32, because in most cases user-working-directory will be pointing to current-working-directory of a system process (cwd).

When you need to determine the current working directory in Java application running on Windows, you could check the following example snippets (probably one should work just fine for you):

Using FileSystems.getDefault() method:

Path path = FileSystems.getDefault().getPath("").toAbsolutePath();

Using getClass().getProtectionDomain().getCodeSource() method:

URL location = WorkingDirectory.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());

Using getPath(".") method:

Path path = FileSystems.getDefault().getPath(".");

4. Conclusion

In this article, we presented how to get the current working directory in Java applications. Unfortunately dedicated system property (user.dir) that should hold a current working directory works a little bit different in Windows and Linux operating systems. That because in Windows Java applications are handled by the system and running as a process using C:\WINDOWS\system32 libraries. Depending on how you start your Java program in Windows you will need to use a different approach. You can start by checking our example snippets available also on GitHub.

{{ message }}

{{ 'Comments are closed.' | trans }}