I'm running a jar file in various locations and I'm trying to figure out how to get the location of the jar file that is running.
In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName. class . getProtectionDomain() .
Introduction. JAR file is a file format based on the popular ZIP file format and is used for aggregating many files into one. A JAR file is essentially a zip file that contains an optional META-INF directory. A JAR file can be created by the command-line jar tool, or by using the java.
jar -tvf file_name.jar. above will only print names of the files. To view the content of files, you can extract the files in a folder by: jar -xvf file_name.jar. this will unzip jar file & put the content in same directory where you are running this.
You can always extract the source files (Java files) of a jar file into a zip. location on your system. Drag and drop the jar for which you want the sources on the JAD. 3 JAD UI will open with all the package structure in a tree format.
Not a perfect solution but this will return class code base’s location:
getClass().getProtectionDomain().getCodeSource().getLocation()
As noted in the comments, this is not a direct answer to the question, but may actually be what many people are looking for (the current directory of the executing code):
new File(".").getAbsolutePath()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With