Logo

Programming-Idioms

  • JS
  • Java

Idiom #106 Get program working directory

Assign to string dir the path of the working directory.
(This is not necessarily the folder containing the executable itself)

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

The "user.dir" property will return the user working directory.
import java.io.File;
String dir = new File("").getAbsolutePath();

"... If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned."
String path = this.getClass().getClassLoader().getResource("").getPath();
let dir = process.cwd ()

Only works in NodeJS because JavaScript in the browser does not know about your directories.
with Ada.Directories; use Ada.Directories;
Dir : String := Current_Directory;

New implementation...
< >
programming-idioms.org