Java – Get the name or path of a running 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()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
// non-static
String jarPath = getClass()
.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
Sample output.
Terminal
/home/mkyong/projects/core-java/java-io/target/java-io.jar
1. Get the path of running JAR
1.1 Create an executable JAR file.
pom.xml
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mkyong.io.howto.resources.TestApp</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
1.2 Run the below code to get the name or path of the running JAR file.
TestApp.java
package com.mkyong.io.howto.resources;
import java.net.URISyntaxException;
public class TestApp {
public static void main(String[] args) {
TestApp obj = new TestApp();
try {
// Get path of the JAR file
String jarPath = TestApp.class
.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
System.out.println("JAR Path : " + jarPath);
// Get name of the JAR file
String jarName = jarPath.substring(jarPath.lastIndexOf("/") + 1);
System.out.printf("JAR Name: " + jarName);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
Output
Terminal
$ mvn clean package
$ java -jar target/java-io.jar
JAR Path : /home/mkyong/projects/core-java/java-io/target/java-io.jar
JAR Name: java-io.jar
2. toURI()?
If the file name or file path contains special characters, for example, %
, the .getLocation.getPath()
will encode the special characters.
try {
// return raw, no encode
String jarPath = TestApp.class
.getProtectionDomain()
.getCodeSource()
.getLocation()
.toURI()
.getPath();
System.out.println("JAR Path : " + jarPath);
// url encoded
String jarPath2 = TestApp.class
.getProtectionDomain()
.getCodeSource()
.getLocation() //.toURI
.getPath();
System.out.println("JAR Path 2 : " + jarPath2);
} catch (URISyntaxException e) {
e.printStackTrace();
}
Output
Terminal
$ java -jar java-io%test.jar
JAR Path : /home/mkyong/projects/core-java/java-io/target/java-io%test.jar
JAR Path 2 : /home/mkyong/projects/core-java/java-io/target/java-io%25test.jar
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-io
As always, great resource, thanks!
Hi mkyong,
It doesn’t work in jboss eap 7. The returned path contains JBOSS_HOME/bin/content… and this path does not exist !!
Hi, mkyong
It does not work on Windows because
TestApp.class.getProtectionDomain().getCodeSource().getLocation()
returns the URLrsrc:./
.I tried it on Windows 10 with JDK 8.
I tested it again, it works.
Windows 10 with JDK 11
Do you have more information?
Hi! On Windows 10 Pro 64bit, and Open JDK 11, I have slashes instead backslashes:
JAR Path : /C:/Users/me/Documents/NetBeansProjects/mavenproject1/target/mavenproject1-1.0-SNAPSHOT.jar
and unnecessary slash at the beginning.
This works good in my case:
System.getProperty(“java.class.path”)
I think you should specify that SDK 11 is needed for this to work :/