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
posted
0
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
posted
0
hm, i think the properties file is on the classpath, this is the manifest.mf ot the jar file:
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
posted
0
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:
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:
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.