tags:

views:

98

answers:

4

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.

A: 

If the jar was launched from its own directory (i.e. java -jar File.jar), the following will do:

System.getProperty("user.dir");

Another method would be

new File (".").getCanonicalPath();

getCanonicalPath() does a few useful things. From the docs:

This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

aioobe
The current directory may well have nothing to do with where the executing jar is located. cd /tmp; java -jar /usr/local/jar/gogo.jar, for example.
Will Hartung
I see, let me think about it...
aioobe
I thought "user.dir" returned the user's home directory?
Michael Angstadt
@mangst: Nope, user's home dir would be `user.home`
Jonik
API docs provide more info: http://java.sun.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29
Jonik
+11  A: 

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

getClass().getProtectionDomain().getCodeSource().getLocation()
David Relihan
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
So this solution will actually return `/otherfolder`?
aioobe
@aioobe: Exactly.
BalusC
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
+1 I twiddled too long with my IDE
stacker
+2  A: 
new File(".").getAbsolutePath()
Bozho
+1 Ah that's clever!
David Relihan
As @Will Hartung said, "The current directory may well have nothing to do with where the executing jar is located."
aioobe
+1  A: 

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