Monday, January 14, 2013

Get the path of a running jar file

When code is running inside a jar file, say foo.jar, and we need to know, in the code, in which folder the running foo.jar is use this command in the Java code.


 return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
Another of achieving the same is using the following code:
String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
Reference:
http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file

No comments:

Post a Comment