In Java, we can use System.getProperty("user.dir") to get the current working directory, the directory from where your program was launched.

  String dir = System.getProperty("user.dir");

  // directory from where the program was launched
  // e.g /home/favtuts/projects/core-java/java-io
  System.out.println(dir);

One of the good things about this system property user.dir is we can easily override the system property via a -D argument, for example:

java -Duser.dir=/home/favtuts/ -jar abc.jar

1. Working Directory

The below program shows different ways like FilePathsFileSystems, or system property to get the current working directory; all methods will return the same result.

CurrentWorkingDirectory.java

package com.favtuts.io.howto;

import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Paths;

public class CurrentWorkingDirectory {

    public static void main(String[] args) {
        
        //printCurrentWorkingDirectory1();
        //printCurrentWorkingDirectory2();
        //printCurrentWorkingDirectory3();
        printCurrentWorkingDirectory4();

    }
    
    // System.Property 
    private static void printCurrentWorkingDirectory1() {
        String userDirectory = System.getProperty("user.dir");
        System.out.println(userDirectory);
    }

    // Path, Java 7
    private static void printCurrentWorkingDirectory2() {
        String userDirectory = Paths.get("")
            .toAbsolutePath()
            .toString();
        System.out.println(userDirectory);        
    }

    // File("")
    private static void printCurrentWorkingDirectory3() {
        String userDirectory = new File("").getAbsolutePath();
        System.out.println(userDirectory);
    }

    // FileSystems
    private static void printCurrentWorkingDirectory4() {
        String userDirectory = FileSystems.getDefault()
            .getPath("")
            .toAbsolutePath()
            .toString();
        System.out.println(userDirectory);
    }
}

Output

/home/favtuts/projects/core-java/java-io
/home/favtuts/projects/core-java/java-io
/home/favtuts/projects/core-java/java-io
/home/favtuts/projects/core-java/java-io

Normally, we use the System.getProperty("user.dir") to get the current working directory.

2. Working Directory for JAR file?

Do not use System.getProperty("user.dir")File or Path to access a file that is inside a JAR file, it is not going to work. Instead, we should use getClassLoader().getResourceAsStream().

// get a file from the resources folder, root of classpath in JAR
private InputStream getFileFromResourceAsStream(String fileName) {

    // The class loader that loaded the class
    ClassLoader classLoader = getClass().getClassLoader();
    InputStream inputStream = classLoader.getResourceAsStream(fileName);

    // the stream holding the file content
    if (inputStream == null) {
        throw new IllegalArgumentException("file not found! " + fileName);
    } else {
        return inputStream;
    }

}

Note

The above code is extracted from this Read a file from resources folder. Refer to example 2 for accessing a file that is inside a JAR file.

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/howto

References

Leave a Reply

Your email address will not be published. Required fields are marked *