• Post Reply Bookmark Topic Watch Topic
  • New Topic

Absolute/Relative path of jar file?  RSS feed

 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there a way to know the absolute/relative path of the executable jar file at run time. Using java.util.jar classes I want to access the contents of the jar file, which is used to launch the application, at run time.
I am developing in windows env.
Thanks
Ashish
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get the absolute path to where the JAR file is located by using -
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this same exact problem in my apps. I wanted to be able to load the version from the manifest in the jar file. This meant I had to have access to the jar file to load hte manifest.
What Nathan said will work, but only if the jar was launched from the directory the jar sits in. It won't work if the jar was launched from another dir and probably won't work if the .jar was clicked to execute.
Example 1:
foobar.jar sits in c:\java\apps
it is launched by typing java -jar foobar.jar in the c:\java\apps dir
user.dir returns c:\java\apps and everything is fine
Example 2:
foobar.jar sits in c:\java\apps
it is launched by typing java -jar c:\java\apps\foobar.jar from the c:\ dir
user.dir returns c:\ which doesn't help us know where the .jar is
I came up with some code fro my apps that tells me exactly where the .jar is. Surely there is a better way to do it than this, but this seems to work. The only time I have found that this code does not work is when an additional path/jar is used on the classpath. If hte .jar is launched with nothing else, it works. Here is the code is anyone wants to see/use it:
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian!

I'd always used "user.dir", which works with executable JARs when you click on them (in windows at least... I haven't tried it yet on another OS, I'll have to give it a try on my Linux box later... ).

From now on I'll use your version! It looks a lot more robust, and works for all cases that might invoke the JAR file.
 
Ashish Gupta
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian and Nathan,
I did figured out to use what Nathan had suggested after I posted my quesiton. Will give try to Brian's solution.
Thanks
Ashish
 
Brian Pipa
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just an FYI - the code I posted works on Windows, linux, and MAC OSX (I've tested it). Again, it breaks if you add anything else to the command line classpath but if you just run the .jar - it works. Someday I'll fix it to handle the extra .jar on the classpath case.
Brian
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!