Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the directory that the currently executing jar file is in? [duplicate]

Tags:

java

jar

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.

like image 847
darrickc Avatar asked May 14 '10 20:05

darrickc


People also ask

How do I find the path of a JAR file?

In Java, we can use the following code snippets to get the path of a running JAR file. // static String jarPath = ClassName. class . getProtectionDomain() .

What is jar directory?

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.

What is jar TVF command?

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.

Can you access source code of JAR file?

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.


2 Answers

Not a perfect solution but this will return class code base’s location:

getClass().getProtectionDomain().getCodeSource().getLocation() 
like image 112
Robben_Ford_Fan_boy Avatar answered Sep 21 '22 06:09

Robben_Ford_Fan_boy


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() 
like image 45
Bozho Avatar answered Sep 18 '22 06:09

Bozho