7 Replies Latest reply on May 14, 2014 6:30 AM by skytrace

    Set path to file in jar


      Sorry for the noob question, but could you tell me please what I do wrong when I trying to set path to file in my jar app.

      I have application which work with xml file. I have next project structure:


      |_  /lib

      |_  /resources/file.xml

      |_  /src


      So, I need to set correctly path to my jar file, because when I running it from IDE, it works nice.

      By default it seems:

      private File file;
      private StreamResult streamResult;
      file = new File("resources/file.xml");
      streamResult = new StreamResult(file);


      And methods where I can modify the DOM structure via transformer in end of methods:

      transformer.transform(source, streamResult);


      So, I trying set path to file:

      private URL url;
      file = new File(url.getPath());
      streamResult = new StreamResult(file);


      But it didn't not work, because when I trying to get resource by next condition

      url = getClass().getResource("resources/file.xml");

      url = getClass().getResource("resources/file.xml");url = getClass().getResource(url = getClass().getResource("resources/file.xml");resources/file.xml");

      url - is null



      I tried next solution



      InputStream input = getClass().getResourceAsStream("resources/file.xml");



      There, I got also null....

      This solutions I found here


      Also, I tried made absolute path


      filePath = file.getAbsolutePath();
      file = new File(filePath);
      streamResult = new StreamResult(file);


      But it also didn't work. There I got message seems like: "Can't find resource /User/user1/Desktop/program1/resources/file.xml" - but that's really absoulte path to a file.


      Also, I tried made it via System.getProperty

      String filename = "file.xml";
      String workingDir = System.getProperty("user.dir");
      finalfile = workingDir + File.separator + "resources" + File.separator + filename;
      file = new File(finalfile);


      I also made unit test which completed with "green light", but in jar its wrong with message "Can't find resource /User/user1/Desktop/program1/resources/file.xm"


      Could you tell me please what I do wrong?

        • 1. Re: Set path to file in jar

          Read this carefully: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

          Then comply:

          1. getResource("/resources/file.xml");


          1. getResourceAsStream("/resources/file.xml"); 
          1 person found this helpful
          • 2. Re: Set path to file in jar

            Yes, I used it, but it didn't work. In running from IDE it works, but not from JAR

            Here, I also tried to replace file.xml to /src category


            But even in this choice, it didn't work from JAR

            • 3. Re: Set path to file in jar

              Not sure what happens, but if your file.xml is indeed in the jar at the right location, don't even bother with file IO (new File()...).  getResource or getResourceAsStream is the right and only way. One more thing I can think of: Java is case sensitive when it comes to resources, so maybe this is the problem? 

              • 4. Re: Set path to file in jar

                You have to provide the correct filename and path.


                If this is really the file name:

                |_  /resources/file.xml

                Then why are you getting an exception that uses this name?

                Can't find resource /User/user1/Desktop/program1/resources/file.xm

                It is looking for a file with an 'XM' extension; not an 'XML' extension.

                • 5. Re: Set path to file in jar

                  Okay, I rewrite my app and I started from the very beginning, all functions are works correctly with CRUD operations. Now, I got the same error, but now I made it like that:

                  public MyConstrucor() {

                          url = getClass().getClassLoader().getResource("file.xml");

                          file = new File(url.getFile());

                          result = new StreamResult(file);



                  In my consructor, I placed some initilizations, where I made a path via getResource.

                  From IDE, it works nice as in previous times, but in JAR, now I have an error in modal dialog window after running my .jar

                  C:\Users\user1\Desktop\file:\C:\Users\user1\Desktop\myapp.jar!\psswd.xml (syntax error in file name or directory name)


                  And it's realy interesting for me...

                  What is it in one part of message "C:\Users\user1\Desktop\file:\" ? I see it's duplicate two times, but why I can't understand... I would have understood if it will be in IDE too, but not! It's beginning only running from JAR

                  • 6. Re: Set path to file in jar

                    file:\C:\Users\user1\Desktop\myapp.jar!\psswd.xml is what getResource() delivers to you as the URL of the jar entry (notice I did not say file, but jar entry).

                    The meaning is: the entry psswd.xml found in the root of the jar C:\Users\user1\Desktop\myapp.jar.

                    Now, ignoring my advice, you want to turn this into a file. So Java places in front of it the current directory. If you use Windows explorer, do you have such a file?

                    psswd.xml is simply not a file. Anyway, since your StreamResult is supposed to write something there, it cannot be in the jar. You cannot write into a jar entry. Jars are supposed to be read only.

                    It's like an EXE writing inside itself. Therefore psswd.xml should be outside the jar, a true file and forget about the resource stuff. It happened to work in the IDE because there it was not jarred yet.

                    It happened to be both a Windows file and a Java resource.

                    • 7. Re: Set path to file in jar

                      Baftos, Thank you so much for your answers, they very help me to understand the principles. I misunderstood the system jar and writings into a jar. I made to determine of OS (*nix or windows) and made some mechanism to save my storage file (.xml) in directory of OS, and now it works. Thanks a lot!