1

I have a java application in Eclipse that uses an eml file like so

File matches = new File("matches.xml");

The file is located in the default package as all the other classes. When I create the JAR it bundles in the XML file with it. My application require me to be able to make changes to the XML file. How can I set it up so the JAR can reference the XML file outside of itself?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dunika
  • 220
  • 2
  • 13

3 Answers3

1

If you're using new File("matches.xml") that won't use a file within a jar file at all. It will only look on the external file system.

If you need to be able to use an external file if it's present, or the version in the jar file as a fallback, you'll need to test for the file's existence (File.exists()) and use Class.getResourceAsStream("matches.xml") for the fallback behaviour.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

My application require me to be able to make changes to the XML file.

Then you will need to extract it from the Jar and save it somewhere on the local file system.

See How can an app use files inside the JAR for read and write? for more details.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

As you want to keep the file outside the jar and want to update it so the jar can read, so you can put the file in the same directory where the jar is and use the following code to access the file

FileInputStream file = new java.io.FileInputStream("matches.xml"); 

So this can be the directory structure.

- matches\
    - matches.jar
    - matches.xml
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • That is a very fragile way to form the `File`. It won't ever work for an applet or an app. deployed using JWS, or for an app. launched from a batch file that does not `cd` to the app. directory.. – Andrew Thompson May 13 '13 at 14:59