Sign in to follow this  
  • entries
    23
  • comments
    31
  • views
    54920

Java - Getting The Path Of The Jar File

Sign in to follow this  

5468 views

Whenever you are working with files in Java, sometimes you need to get the full path to the directory the .jar file is located in. Most of the time you can just use
.String path = "./File_Name.txt"; // Note the "." before the "/"
.
when dealing with reading/writing/creating files and directories - HOWEVER for those times when that is not enough, you'll need to extract a "clean" path to the .jar file.
Below is a simplified example that does exactly this. I cut out all of the unnecessary, over complicated, and/or outdated items you may find in other posts about this subject.
I will explain how it works as we go along.
.public class Main { public static void main (String[] args) throws Exception { String i; // ignore - not important String pathName; // path to create new file String fileName; // name of new file File file; // File object to create i = "\n**********\n"; // ignore - not important fileName = "/Test_File.txt"; // set the file name pathName = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath(); // get the path of the .jar pathName = URLDecoder.decode(pathName,"utf-8"); // convert the path format from HTML to UTF pathName = pathName.substring(1,pathName.lastIndexOf("/") ); //create a new string by removing the garbage System.out.println(i + pathName + fileName + i ); // this is for debugging - see the results file = new File(pathName + fileName); // create file file.createNewFile(); // make new file }}
.
.
.pathName = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
.
This returns the *raw* path of the .jar file in a HTML format with some extra junk that is not needed. Using ".toURI()", ".toURL()" and ".toString() is not necessary AND will create more junk in the path name!
.[quote]

C:\Users\RICK\Desktop>java -jar test1111.jar

**********
/C:/Users/RICK/Desktop/Test1111.jar
**********
[/quote]
.
Note: If you have any directories with special characters in them, you will end up with the HTML equivalent ... "%20" instead of " " for example.
The next line parses the HTML format into UTF, which Java can understand as a path name
.pathName = URLDecoder.decode(pathName,"utf-8");
.
This cleans up the HTML format, but leaves some junk behind
.[quote]

C:\Users\RICK\Desktop>java -jar test1111.jar

**********
/C:/Users/RICK/Desktop/Test1111.jar
**********[/quote]
.
Now we need to remove the junk, and create a usable path for Java to use
.pathName = pathName.substring(1,pathName.lastIndexOf("/") );
.
This removed the first "/" from the string, and also cuts off the name of the .jar file !

This creates a usable path name, to be used, in this example, to make a file
.file = new File(pathName + fileName); file.createNewFile();
.
.[quote]

C:\Users\RICK\Desktop>java -jar test1111.jar

**********
C:/Users/RICK/Desktop/Test_File.txt
**********[/quote]
.
Thank you for reading. Please leave feedback below !
Sign in to follow this  


4 Comments


Recommended Comments

Why couldn't you just use new File("").getAbsolutePath();?

Does this not return you the same thing assuming all your code is running in a single jar?

Share this comment


Link to comment

Why couldn't you just use new File("").getAbsolutePath();?

Does this not return you the same thing assuming all your code is running in a single jar?

.

That is completely missing the point.  If I wanted to insert the path directly, I'd use "./"

Share this comment


Link to comment

 

Why couldn't you just use new File("").getAbsolutePath();?

Does this not return you the same thing assuming all your code is running in a single jar?

.

That is completely missing the point.  If I wanted to insert the path directly, I'd use "./"

 

And what the point would be then if not getting the path of different .jars in the same application?

 

For that purpose I understand the boilerplate (if you have different .jars in different folders running under the same application), otherwise I dont.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now