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.
Code
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
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.JSONPath TesterTest JSONPath expressions against JSON documents with dot notation, brackets, wildcards, and recursive descent.SVG Path VisualizerParse SVG path d attributes (M/L/H/V/C/S/Q/T/A/Z), render a scaled wireframe preview with vertices and control handles, and compute bounding boxes.
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 =`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.