I want to access my current working directory using java.
My code :
String current = new java.io.File( \".\" ).getCanonicalPath();
System.ou
Use CodeSource#getLocation().
This works fine in JAR files as well. You can obtain CodeSource
by ProtectionDomain#getCodeSource() and the ProtectionDomain
in turn can be obtained by Class#getProtectionDomain().
public class Test {
public static void main(String... args) throws Exception {
URL location = Test.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
}
}