Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get application filepath

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Have you tried the "user.dir" property from the Systems class?

Henry
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Allasso Travesser
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Can't .... do .... plaid .... So I did this tiny ad instead:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic