views:

24

answers:

1

Hey there

My application requires a XML file to work and it doesn't even start without the file. Why I bundle my app as a JAR file it works fine as long as the XML file is placed in the same directory as the JAR file.

When I'm exporting the project as an OS X application package, the application does not work. If I copy the XML file in the same directory where the application package is, it works.

So I'm pretty sure that it is a minor addressing problem to access the XML file from within my Java code. I'd like to put the XML file into the application package. Simply copying it in the same directory where the JAR file is does not work.

The file, or better the files are addressed like this: doc = sax.build("file.xml"); and are located directly in my project folder. I'm working with Eclipse and I export my project directly from Eclipse as an application bundle. I also tried it with the OS X Jar Bundler, which delivers the same result.

So, how do I address my files correctly, so that I can place them into the application bundle?

Any help is appreciated! Thank you very much!

+1  A: 

You are most likely reading it in as a physical file, which needs to be located in the current working directory.

Have you considered reading it in as a resource instead which allows it to be found via the classpath?

Thorbjørn Ravn Andersen
I haven't worked with resource files yet. Any advice on how to do that, and where to place the file?
Peter
You probably want to use http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)Note, to load build.xml from the root instead of next to the class doing the loading, use "/build.xml". It may be in your jar files or in your file system (if you have "." in your classpath)
Thorbjørn Ravn Andersen
Sorry for being stupid, but I really can't figure how to do that.I do realize that I can read files with the getResource() or getResourceAsStream() method, but I still don't know how to access the explicit file and where to put it in my project folder.As I said, I never done that before and I can't find any helpful information on the web.Another problem is, that my XML file is modified within my code, so only reading the resource won't be enough to solve my problem.
Peter
I tried your your suggestion with getClass().getResource("/file.html"); for my html help page, which works fine within my IDE, but as soon as I export my project as an application bundle, the file can't be accessed.
Peter