File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Embedding config file in a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Embedding config file in a jar" Watch "Embedding config file in a jar" New topic
Author

Embedding config file in a jar

Don Stadler
Ranch Hand

Joined: Feb 10, 2004
Posts: 451
Recently I've been trying to embed an xml configuration file into a jar file and cannot seem to understand how the classloader handles this. The file is located in <project root>/config of the jar. I am trying to read the contents using:

InputStream periodicalsXml = this.getClass().getResourceAsStream( "config/periodicallist.xml");

But I get a null InputStream.

I gather that something needs to be done to the MANIFEST.MF file (to define the classpath???), but the explanations I have been reading are rather inscrutable on the point. My current MANIFEST.MF file is minimal:

====================================================================
Manifest-Version: 1.0
Created-By: 1.3.1 (Apple Computer, Inc.)
=====================================================================

What I am trying to do should be rather simple but has not proven so. I want to stuff this xml config file into a DOM and then play with it - but cannot unless I can find it! Can anyone explain what needs to be done or point me to a URL which explains this well (this is a regular class, not an applet, BTW).

Thanks in advance....

Don
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Originally posted by Don Stadler:
The file is located in <project root>/config of the jar. I am trying to read the contents using:

InputStream periodicalsXml = this.getClass().getResourceAsStream( "config/periodicallist.xml");
If the class containing that code is in package a.b.c then your code will be looking for /a/b/c/config/periodicallist.xml, and that probably isn't what you want to happen. I don't know what "project root" means in your question, but if you don't want to look for the config file relative to the class that's doing the looking, then use an absolute path for the config file. That would look something like this, depending on the actual directory structure inside your jar file:Nothing to do with the manifest.
Don Stadler
Ranch Hand

Joined: Feb 10, 2004
Posts: 451
Thanks Paul.

So if the jar was named 'root' and had a subdirectory named 'config' this should work?

[ April 10, 2007: Message edited by: Don Stadler ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

The name of the jar is irrelevant. You need to provide the full path in the directory structure inside the jar.
 
 
subject: Embedding config file in a jar
 
Threads others viewed
How to Jar?
Refering a jar file from another jar
Accessing file inside JAR
Manifest file problems
Read MANIFEST-MF files that are with in jars that exists in jars with in WEB-INF/lib
MyEclipse, The Clear Choice