• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 23958
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: 22821
132
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.

 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic