There's the best way I've found for an application to find the path of the folder containing its code:
Here's the code:
Note: If you search for other solutions to this problem, you'll find many people telling you to just call System.getProperty("user.dir"). This doesn't work in general, because it merely returns the current directory, not the directory that contains your application's code.
public static String getRootPath() {
String rootPath;
try {
rootPath =
URLDecoder.decode(FileUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8");
String osName = System.getProperty("os.name");
if (osName.toUpperCase().contains("WINDOWS")) {
rootPath = rootPath.substring(1);
}
rootPath = new File(rootPath).getParent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
// If an error occurred, default to current directory.
rootPath = System.getProperty("user.dir");
}
Globals.getLogger().info(MessageFormat.format("getRootPath: {0}", rootPath));
return rootPath;
}
| Title | Date |
| Get JetBrains IDEs to Remember Subversion (SVN) Credentials | November 3, 2026 |
| Converting a Thinkpad from Windows 11 to Linux Mint | October 29, 2026 |
| Raspberry Pi Camera Trap and Animal Detection Program | August 29, 2026 |
| Python Tip: Fix Incorrect Orientation of Digital Photos | August 8, 2026 |
| EBT Weather is now available for Windows and Linux | May 30, 2026 |
| Node.js + Express: How to Block Requests by User-Agent Headers | January 7, 2026 |
| Vault 3 is Now Available for Windows on ARM Machines! | December 13, 2025 |