Eric Bergman-Terrell's Blog

Java Programming Tip: Get the Application Folder
January 19, 2009

There's the best way I've found for an application to find the path of the folder containing its code:

  1. Call getProtectionDomain().getCodeSource().getLocation().getPath() on one of your application's classes.
  2. URL Decode the results, otherwise it will have %20 instead of spaces if one or more of your folder names have embedded spaces.
  3. If the program is running on Windows, it will need to remove the initial "/" from the path.

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("");

if (osName.toUpperCase().contains("WINDOWS")) {
rootPath = rootPath.substring(1);

rootPath = new File(rootPath).getParent();

} catch (UnsupportedEncodingException e) {

// If an error occurred, default to current directory.
rootPath = System.getProperty("user.dir");

Globals.getLogger().info(MessageFormat.format("getRootPath: {0}", rootPath));

return rootPath;
Keywords: Java, Application Folder, Directory, Windows, URL Decoding

Reader Comments

Comment on this Blog Post

Recent Posts

EBTCalc Users: A Desktop Version is Now Available!October 25, 2019
Python Script to Audit MediaMonkey TranscodingAugust 15, 2019
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016