aspose file tools
The moose likes Java in General and the fly likes get application filepath Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "get application filepath" Watch "get application filepath" New topic
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
    
  19


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.

 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: get application filepath
 
Similar Threads
Wierd file overwrite problem.
IllegalStateException
include and file path
java io
Interceptor not working