0

How do I extract the folder of the running Jar file? The following code gives me the path to the jar itself -- Eg. C://MyProgram/myJar.jar ... but I want only the path to the folder it is located in, so C://MyProgram/

new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()) 

I have now tried one suggestion of using getParent() as such:

Path p = new Path(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
File f = new File(p.toString());

But Java throws an exception as the path cannot be parsed due to illegal characters in the path.

Jack Avante
  • 1,405
  • 1
  • 15
  • 32
  • How should your IDE know where your JAR is? – dan1st Apr 02 '21 at 20:08
  • The IDE is quite irrelevant in this, my bad. I set my working directory in the IDE manually. The question is specifically oriented at knowing the Jar's location when it's run directly using Java or from the console using the java -jar command – Jack Avante Apr 02 '21 at 20:24
  • And what directory does `new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/data/");` show? Under what circumstances is it wrong? – dan1st Apr 02 '21 at 20:28
  • It was a bit hard to test since it is a GUI app and the problem only manifested after compiling and running it directly. I added a label in the GUI to show the path and I found the problem.. the getPath() above actually points to the jar itself, including the jar file. To fix it I need to remove the file itself from the path and just leave the directory where it is located – Jack Avante Apr 02 '21 at 20:33
  • Could it be that your question is a duplicate? Take a look at this one. [How to get the path of a running JAR file?](https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file) –  Apr 03 '21 at 16:41
  • Potentially yes, I didn't realize that that would return the parent directory since I've tried something similar recently and it retrieved the file of the jar itself. Though another issue sparks with the fact that I am trying to initialize this in a static context as a constant to be available everywhere, and this the answer in that questions throws an exception, which I'm not sure how to handle from a static context, but that's a new different problem. I'll vote this question to close as duplicate – Jack Avante Apr 03 '21 at 16:48

1 Answers1

1

I think you are looking for method getParent() in Path class. So, your code should look like this:

System.out.println(Path.of(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()))
Kacper
  • 451
  • 6
  • 17
  • The getPath() returns a String so I cannot apply .getParent() onto it. If I try to convert it to a path, then get parent and back to string, I get invalid character exception, as it can't parse the ':' character – Jack Avante Apr 03 '21 at 16:17
  • I tried to run it like this: `System.out.println(Path.of(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI()));` And i've got the parent directory not the path to the jar. But Path should be able to parse URI and then u can use getParent() – Kacper Apr 03 '21 at 18:16