Accessing .properties file inside jar exported file

Hi,

I've coded some common utilities inside a Eclipse java project. Inside this common utilities project, under a directory called "resources" , there is a .properties file , that is,"/resources/file.properties".

Now I want to pack these all common functionality into a .JAR file and use it from other projects. I've correctly exported the .JAR file and now I want to access this .properties file from some Java classes I've coded.

The issue is the class I'm executing is located for example under package "org.companyName.projectName" and the exported .jar is located at third-party\lib\companyName".

How should I access from that Java class to .properties file packed inside the .jar? .Jar file's name is commonutilities.jar and from my class seems I have access to commonutilities wthout problems (at least Eclipse's intellisense ays me that).

I would really appreciate any help on this.

Thanks very much in advance.

I've tried everything but nothing works.

[1017 byte] By [lonifasikoa] at [2007-11-27 5:41:13]
# 1

Ion,

Sorry I can't remember exactly, It's one of those things that was delegated to a utilities class well before my time (that code is at work & I'm at home)... but I'm pretty sure it's getResourceAsStream, I just googled for "Java 6 API getResourceAsStream", and apparently it's in the ClassLoader class.

http://java.sun.com/j2se/1.5.0/docs/guide/lang/resources.html says

<quote>

System Resources

A system resource is a resource that is either built-in to the system, or kept by the host implementation in, for example, a local file system. Programs access system resources through the ClassLoader methods getSystemResource and getSystemResourceAsStream.

For example, in a particular implementation, locating a system resource may involve searching the entries in the CLASSPATH. The ClassLoader methods search each directory, ZIP file, or JAR file entry in the CLASSPATH for the resource file, and, if found, returns either an InputStream, or the resource name. If not found, the methods return null. A resource may be found in a different entry in the CLASSPATH than the location where the class file was loaded.

</quote>

hope that helps some.

Keith.

corlettka at 2007-7-12 15:18:26 > top of Java-index,Java Essentials,New To Java...
# 2

Hi Keith,

Thanks for the reply. In fact, I'm using getResourceAsStream method but there is no way to catch ".properties" file located in a directory different from where I'm executing my code.

I've tried to copy .jar in the same directory as the class, and works perfect.

But when having .jar file in other directory, there is no way to access properly.

I just wanted to have my files, jars and project structure in general, correctly ordered, but if this continues giving me headache, I'm afraid I'll change and copy .jar file inside the executing directory.

Thaks very much in advance.

lonifasikoa at 2007-7-12 15:18:26 > top of Java-index,Java Essentials,New To Java...