| Author |
get application filepath
|
Allasso Travesser
Greenhorn
Joined: Feb 06, 2010
Posts: 25
|
|
Hello,
Is it possible and how to get the full filepath of a currently running Java application?
something analogous to $0 in a UNIX script?
Thanks, Allasso
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Have you tried the "user.dir" property from the Systems class?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
That's not necessarily the same; user.dir is the folder from which the JVM was started.
Do a search; this question has come up before. In short, you can't get this path directly, but there are some tricks. One uses ProtectionDomain (do a search on Javaworld.com), one uses Class.getResource() using the class itself. For example, in class Test:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Allasso Travesser
Greenhorn
Joined: Feb 06, 2010
Posts: 25
|
|
Rob Prime wrote: In short, you can't get this path directly, but there are some tricks. One uses ProtectionDomain (do a search on Javaworld.com), one uses Class.getResource() using the class itself. For example, in class Test:
Thank you, Rob, yes, they both work.
I found this on Real Gagnon's site this morning demonstrating getProtectionDomain as you said:
http://www.rgagnon.com/javadetails/java-0581.html
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
getProtectionDomain() will throw a security exception however if the security manager doesn't allow it.
|
 |
 |
|
|
subject: get application filepath
|
|
|