snippetjavaCritical
How to get the path of a running JAR file?
Viewed 0 times
howrunningjarthefilepathget
Problem
My code runs inside a JAR file, say foo.jar, and I need to know, in the code, in which folder the running foo.jar is.
So, if foo.jar is in
So, if foo.jar is in
C:\FOO\, I want to get that path no matter what my current working directory is.Solution
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).getPath();Replace "MyClass" with the name of your class.
Obviously, this will do odd things if your class was loaded from a non-file location.
Code Snippets
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).getPath();Context
Stack Overflow Q#320542, score: 615
Revisions (0)
No revisions yet.