Current Working Directory

user.dir anchors relative paths for the whole JVM: Paths.get("").toAbsolutePath() lands on the same directory that System.getProperty("user.dir") reports.

JavaCli Tooling

Code

snippet
import java.nio.file.Path;
import java.nio.file.Paths;

public class WorkingDirectory {

    public static void main(String[] args) {
        // user.dir 属性:JVM 启动时所在的 current working directory
        String fromProperty = System.getProperty("user.dir");

        // 空相对路径锚定到 user.dir:working directory 就是相对路径的解析基准
        Path fromEmpty = Paths.get("").toAbsolutePath();

        System.out.println("user.dir    = " + fromProperty);
        System.out.println("empty path  = " + fromEmpty);

        // 相对路径按同一基准解析,与进程从哪里被拉起无关
        Path config = Path.of("config", "app.properties");
        System.out.println("config path = " + config.toAbsolutePath().normalize());
    }
}

How it works

Everything happens inside the class WorkingDirectory and the function main.

Points of care

  • The stock toolchain runs it — nothing extra required.

Putting it to use

Entry signature: public static void main(String[] args)

The run prints user.dir =, empty path =, config path = — each prefix names the step that produced it.

Core lines

String fromProperty = System.getProperty("user.dir");
Path config = Path.of("config", "app.properties");
Path fromEmpty = Paths.get("").toAbsolutePath();

Interactive Tools for This Topic

FAQ

What does Current Working Directory print when run?
Run it as a complete program — the output lines carry labels such as `user.dir =`, `empty path =`, `config path =`.