06 March 2017
Printing current path
When coding in Java I found myself having to print the current path of the executing JVM program in order to troubleshoot File not Found exceptions and other file-system related issues.
There are different ways to do that in Java:
1. Using System properties
You can use System class getting the user.dir property to achieve this:
System.out.println("Current path using System = " + System.getProperty("user.dir"));
2. using NIO Path
In Java 7 and beyond you can use NIO Path to get current path:
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current path using NIO is: " + s);