I'm running a jar file in various locations and I'm trying to figure out how to get the location of the jar file that is running.
5 Answers
Not a perfect solution but this will return class code base’s location:
getClass().getProtectionDomain().getCodeSource().getLocation()

- 8,494
- 11
- 64
- 85
-
19Not perfect? This is actually the most reliable since the other proposed suggestions returns the **current working directory** which is not per se the folder where the JAR is located. E.g. if you do `cd /somefolder` and then `java -jar /otherfolder/file.jar` then other suggestions would return `/somefolder`. +1. – BalusC May 14 '10 at 20:21
-
1
-
3It isn't always possible to determine the location of the enclosing JAR (class loaders can use anything for storage, not just a file system), but when it is, *this* is the way to do it. – erickson May 14 '10 at 20:26
-
-
@erickson Does that depend on the JVM you're using? I'm trying to figure exactly when this won't work. – gsgx Apr 11 '13 at 18:20
-
@gsingh2011 If someone wrote a custom classloader, not a `URLClassLoader`, the `CodeSource` of its classes could be `null`. I haven't tested it, but I suppose that a tool like ASM, which allows you code programmatically construct a new class in a running JVM, would not assign a code source to its classes. And if it did, any location would probably be meaningless. – erickson Apr 12 '13 at 00:12
-
1
-
this is also actually the first solution on SO that worked for me on a Linux system – dasLort Sep 16 '15 at 11:25
-
**NOTE:** to future readers, `...getCodeSource().getLocation()` returns a `URL` and `URL.toString()/getPath()/getFile()/etc..` **replaces spaces with `%20`s** (as expected in a url). If you need to keep the plain spaces then use `...getLocation().toURI().getPath()`. – xtratic Aug 24 '18 at 19:11
As noted in the comments, this is not a direct answer to the question, but may actually be what many people are looking for (the current directory of the executing code):
new File(".").getAbsolutePath()
-
22As @Will Hartung said, "The current directory may well have nothing to do with where the executing jar is located." – aioobe May 14 '10 at 20:23
-
1
Here is my method to get execution path from anywhere
private static String GetExecutionPath(){
String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));
absolutePath = absolutePath.replaceAll("%20"," "); // Surely need to do this here
return absolutePath;
}

- 35
- 4

- 384
- 3
- 3
-
you can use URLDecoder.decode(directoryPath, "UTF-8"); to replace %20 with space – Mauro Midolo Jan 15 '17 at 23:53
-
also you can use absolutePath.lastIndexOf(File.separator) to support all operating system – Mauro Midolo Jan 15 '17 at 23:55
Consider the following function
//This method will work on Windows and Linux as well.
public String getPath(){
URL rootPath = getClass().getResource("");
String URI=rootPath.toString().substring(9);
String[] currentPath=URI.split("myjar.jar!");
currentPath[0]=currentPath[0].replaceAll("%20"," ");
return currentPath[0];
}

- 180
- 12
This seems like an iffy question to start out with.
If I am running code in Java, it is running out of rt.jar and probably a dozen other jars.
The very first files to run will actually be in rt.jar, rt.jar it calls your main.
If you write a class that allows you to tell what jar you are running and that class gets moved into a different jar, then you are not "running out of" the jar that contained your main any more.
I think what you want is the command line that started your application, but if you CD'd to the directory first, then it might not have the full path to your jar.
Also, you may not be running from a jar--someone could have expanded your jar into classes in a directory and is running it that way.
Personally I'd just wrap your program in a shell script launcher where you could look at your current location and the location of the jar file and then pass them into java.

- 62,186
- 18
- 105
- 157
-
Good point, our application is wrapped in one huge jar (unfortunately). – darrickc Jan 09 '13 at 14:47