In a nutshell i have a java program that takes parameters from an XML file. I made a .jar file from my java project. Within the java project i have the XML file set to a static directory, i'd like to be able to change that so the jar would look for the XML within the directory it's in. Is there any solution to this? Don't really want to use a file chooser either.

Thanks

Well the parameters of the XML file may need to change, so i'd like to try to avoid having to open the jar to make changes. In the end i suppose the directory could just be static, but i was just wondering if there was a simple answer.

What happens if you try to read the file from a class in the jar file? To see where the program is looking for a file, create a File object with the filename and print out the File object's absolute path.

I think i'd still have to give it the file to be read in, i'm not sure there is a way around this one. Although in the end it might not be a big deal. After all the program itself with be in a static place, so i guess the path could just be set to that, with like a config folder in that directory. I was just trying to think of a way that it could no be broken, maybe i'll build in some error message stating where the file should be if not found in that path.

A user can always put files in the wrong folder or rename part of the path and break the code.

You make a good point! I just figure i should make it at idiot proof as possible, while leaving in the necessary functionality.

This arcane line of code will return a File object representing the jar file itself (or more precisely the jar from which the class for the current object was loaded) ..

new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); 

... from which it's trivial to get the enclosing directory with jarFile.getParent()and use that to reference any other file in that same directory.

This article has been dead for over six months. Start a new discussion instead.