1

I try to retrive a path of a directory where my executable jar file is situated. That means: I have the following structure:

 --> Application (this is a folder somewhere in my file system)
     --> application.jar (this is my java application
     --> SomeData (folder in the same directory like the application)
         --> some other folders
         ......

When I start my application.jar via command line I want to parse some files inside the SomeData folder. In https://stackoverflow.com/a/320595/1540630 they already showed how to get the current path of a running jar file but when I execute the statement:

System.out.println(XMLParser.class.getProtectionDomain().getCodeSource().getLocation().getPath());

...I just get the following:

/.../Application/application.jar

But I just want to have:

/.../Application/

Better to say in later steps I need

/.../Application/SomeData/SomeFolder/data.xml

Can someone help me please?

Community
  • 1
  • 1
Metalhead89
  • 1,740
  • 9
  • 30
  • 58

3 Answers3

4

CodeSource.getLocation() gives you a URL, you can then create new URLs relative to that:

URL jarLocation = XMLParser.class.getProtectionDomain().getCodeSource().getLocation();
URL dataXML = new URL(jarLocation, "SomeData/SomeFolder/data.xml");

You can't simply do new File(...getCodeSource().getLocation().getPath()) as a URL path is not guaranteed to be a valid native file path on all platforms. You're much safer sticking with URLs and passing the URL directly to your XML parser if you can, but if you really need a java.io.File then you can use an idiom like this to create one from a URL.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • The problem is, that i cant just put an URL to one xml to a xml parser. below my SomeData folder there are many other folders and inside these folders there is one xml for each folder. I have to traverse all xml files in all folders. But at first I just want to get to the "root"-folder of the xml files...so this is not the root folder where the application is but it is the folder where all other fodlers and xmls are inside (so for our example this would be the "SomeData" folder – Metalhead89 Jul 23 '12 at 13:54
  • How can I get the URL of my root folder. CodeSource.getLocation() just gives me the URL with the application: ...../Application/application.jar. But this is not useful for me :( – Metalhead89 Jul 23 '12 at 14:04
  • Yes, and once you have a URL to the JAR file you can create other URLs using paths relative to that, just like relative links in an HTML page are resolved against the folder that contains the HTML file - so `new URL(theJarUrl, "SomeData/SomeFolder/")` is the URL `file:/..../Application/SomeData/SomeFolder/`, which you could then convert into a `File` using the technique I linked to at the end of my answer. – Ian Roberts Jul 23 '12 at 15:14
  • And in particular if you want the URL to the `.../Application/` directory that is `new URL(theJarUrl, ".")` – Ian Roberts Jul 23 '12 at 18:34
1

Once you have the jar's location you can use

new File(new File(jarPath).getParent(), "SomeData/SomeFolder/data.xml");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Actually it should work now, like you all said. I wrote the following: String path = XMLParser.class.getProtectionDomain().getCodeSource().getLocation().getPath(); File file3 = new File(new File(path).getParent(), "SomeData"); When I print this it says: ...\Application\SomeData Actually this should be fine but if i loop through the folder i get a nullpointer exeption, which cant be. – Metalhead89 Jul 23 '12 at 13:39
  • Try listing `new File(jarPath).getParentFile().listFiles()` – Peter Lawrey Jul 23 '12 at 13:43
  • Are you trying to run this as an applet? `new File()` only works on the local file system. – Peter Lawrey Jul 23 '12 at 13:58
  • No this is a usual Java application running on the local file system – Metalhead89 Jul 23 '12 at 14:01
  • Is the jar in the current working directory? i.e. is `new File(jarPath).getParentFile() == null` – Peter Lawrey Jul 23 '12 at 14:08
  • No it is not. If I do new File(jarPath).getParentFile() I get the correct path but somehow it is no valid path which whom I can traverse folders or parse xml files – Metalhead89 Jul 23 '12 at 14:10
  • But you can list this path on the command line, as the user which runs the process? – Peter Lawrey Jul 23 '12 at 14:16
  • Of course. If I type the path manually into the source code or if i use the JFileChooser for choosing the "SomeData" folder everything works fine. Also when I just type "SomeData" as parameter for my parsing steps inside Eclipse everythings works fine. The problem is just when I use an executable jar file in a folder somewhere possible in my file system....then everythings goes bad – Metalhead89 Jul 23 '12 at 14:19
  • That doesn't make sense if the path is the same, it doesn't matter where you got it from. I can only assume the path is not the same in some way. – Peter Lawrey Jul 23 '12 at 14:39
  • I think its how Ian Roberts said: "You can't simply do new File(...getCodeSource().getLocation().getPath()) as a URL path is not guaranteed to be a valid native file path on all platforms." – Metalhead89 Jul 23 '12 at 14:57
1

Since you already have the path as a string. You can try the following

String path = XMLParser.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 

String parentFolder = new File(path).getParent();
Varun
  • 1,014
  • 8
  • 23
  • 1
    `path` is the path part of a URL, it isn't necessarily a valid file path (it may be on unix-like systems but won't be on Windows). – Ian Roberts Jul 23 '12 at 13:11