Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

read properties file from jar

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic