aspose file tools*
The moose likes Beginning Java and the fly likes read properties file from jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "read properties file from jar" Watch "read properties file from jar" New topic
Author

read properties file from jar

Felix Jauch
Greenhorn

Joined: Nov 23, 2006
Posts: 3
hi @all

I try to read a properties file with my application which is bundled as jar. The problem is the properties file is outside of the jar, but placed in the same directory, the structure is the following:

<install_directory>
- Application.jar -> with the Main class
- server.properties

How can i read the server.properties from classes in the Application.jar ?

thanks!
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
Make sure the properties file is on the Classpath then you can access it trough getResourceAsStream() / getResource() methods of ClassLoader.
The Stream can be passed to the load() (?) method of Properties. Thats it.

Make sure you close the Stream in a finally clause so the resources are properly freed.

Pascal
Felix Jauch
Greenhorn

Joined: Nov 23, 2006
Posts: 3
hm, i think the properties file is on the classpath, this is the manifest.mf ot the jar file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_08-b03 (Sun Microsystems Inc.)
Main-Class: com.crb.server.Main
Class-Path: server.properties

this is my Code:

Properties p = new Properties();p.load(getClass().getClassLoader().getResourceAsStream("server.properties"));

but the code doesn't work

[ November 24, 2006: Message edited by: Felix Jauch ]
[ November 24, 2006: Message edited by: Felix Jauch ]
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
to be honest... i dont know much about manifest format but i found this:
Download Extensions - version 1.2 only

Download extensions are JAR files that are referenced by the manifest files of other JAR files. See the trail on the extension mechanism for information about extensions.

In a typical situation, an applet will be bundled in a JAR file whose manifest references a JAR file (or several JAR files) that will serve as an extension for the purposes of that applet. Extensions may reference each other in the same way.

Download extensions are specified in the Class-Path header field in the manifest file of an applet, application, or another extension. A Class-Path header might look like this, for example:

Class-Path: servlet.jar infobus.jar acme/beans.jar

With this header, the classes in the files servlet.jar, infobus.jar, and acme/beans.jar will serve as extensions for purposes of the applet or application. The URLs in the Class-Path header are given relative to the URL of the JAR file of the applet or application.


And this is always talking about jar files.


so try something like this when you run your program:

java -cp .;../myjar.jar my.Main

where the -cp (or -classpath) defines the classpath to be used. The dot (.) is current directory (the directory where you execute the java command).

pascal
neetika sharma
Ranch Hand

Joined: May 19, 2006
Posts: 89
Felix Jauch wrote:hm, i think the properties file is on the classpath, this is the manifest.mf ot the jar file:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_08-b03 (Sun Microsystems Inc.)
Main-Class: com.crb.server.Main
Class-Path: server.properties

this is my Code:

Properties p = new Properties();p.load(getClass().getClassLoader().getResourceAsStream("server.properties"));

but the code doesn't work

[ November 24, 2006: Message edited by: Felix Jauch ]
[ November 24, 2006: Message edited by: Felix Jauch ]


In case someone still reads this post for solution.
Manifest.MF file should contain:
Class-Path=.
where dot(.) specifies the current working directory
and put the server.properties file in the same directory as jar file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read properties file from jar