Hey everyone,
Is there a java method that can help me obtain the path of the jar file
of the currently running program (from within the program itself)?
For example, if my jar's name is a.jar I want to be able to print a.jar
in the main app (dynamically of course)
Thomas Fritsch - 13 Jul 2006 12:53 GMT
> Hey everyone,
> Is there a java method that can help me obtain the path of the jar file
> of the currently running program (from within the program itself)?
Which jar file? Usually a Java application has more than 1 jar files.
> For example, if my jar's name is a.jar I want to be able to print a.jar
> in the main app (dynamically of course)
URL url =
YourClass.class.getProtectionDomain().getCodeSource().getLocation();
You should get something like file://yourDirectory/a.jar or
http://www.yourServer.com/yourDirectory/a.jar
But there are issues with this approach:
getProtectionDomain() may throw a SecurityException
getCodeSource() may return null
See also the API docs of
java.lang.Class#getProtectionDomain
java.security.ProtectionDomain#getCodeSource
java.security.CodeSource#getLocation

Signature
Thomas
Paul Davis - 13 Jul 2006 13:23 GMT
This should give you what you are looking for:
String resName = this.getClass().getName().replace('.','/')+".class";
String url = ClassLoader.getSystemResource(resName).getPath();
Just chop up the string to get what you need