27

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.

darrickc
  • 1,872
  • 6
  • 27
  • 38
  • The best choice is: `String path = new File(".").getCanonicalPath().toString();` – NaN Nov 27 '17 at 20:28

5 Answers5

70

Not a perfect solution but this will return class code base’s location:

getClass().getProtectionDomain().getCodeSource().getLocation()
Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85
  • 19
    Not 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
    So this solution will actually return `/otherfolder`? – aioobe May 14 '10 at 20:24
  • 3
    It 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
  • +1 I twiddled too long with my IDE – stacker May 14 '10 at 20:30
  • @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
    for me it returns including program.jar name – GorillaApe Jan 27 '14 at 18:40
  • 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
14

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()
bbarker
  • 11,636
  • 9
  • 38
  • 62
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 22
    As @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
    There is much more smaller code than this. `new File("").getAbsolutePath();` –  Dec 22 '17 at 11:09
7

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;
}
Sygsky
  • 35
  • 4
Vahan Margaryan
  • 384
  • 3
  • 3
2

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];
        }
0

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.

Bill K
  • 62,186
  • 18
  • 105
  • 157