*
The moose likes Java in General and the fly likes Reading properties file in classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading properties file in classpath" Watch "Reading properties file in classpath" New topic
Author

Reading properties file in classpath

veda patil
Greenhorn

Joined: Oct 06, 2005
Posts: 6
How to read the properties file in classpath.

private static String fileName = "ApplicationConfig.properties";
ClassLoader.getSystemClassLoader().getResourceAsStream(fileName)

I have set classpath variable to :
classpath = D:\ApplicationConfig.properties

But getting error.

Please let me know how to read a properties file in classpath
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

An easier way to do this is to use ResourceBundle.getBundle().

If you want to get a resource as a stream, this quote from the JavaDocs might help:

The name of a resource is a '/'-separated path name that identifies the resource


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Don't put the properties file itself in the classpath, but just the directory that contains the file. So, instead of this:

classpath = D:\ApplicationConfig.properties

use this:

classpath = D:\


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
veda patil
Greenhorn

Joined: Oct 06, 2005
Posts: 6
Both above solution are working fine if i am running the program.
But if i build the exe file and the run the application i am getting :
Caused by: java.util.MissingResourceException: Can't find bundle for base name
pplicationConfig, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundl
.java:804)

Thanks
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
> if i build the exe file and the run the application

What part of java builds an exe file?
If you are using another application for this then you need to read the doc for that app to see how to use it.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3702
    
    5

I spent like a month researching once this issue as it relates to J2EE jars and it can be frustrating. You can put the properties file inside a JAR, but then the JAR must be specifically named in the class path if it is being used as a utility jar and not the main application.

I tend to use getResourceAsStream() more than RS.getBundle() due to easier naming conventions, although both work just fine. Show the error you get if you use getResourceAsStream() with D:\ as the classpath.


My Blog: Down Home Country Coding with Scott Selikoff
ram dhan
Greenhorn

Joined: May 16, 2013
Posts: 1
Thank you so much Jesper de Jong .

my classpath issue was resolved . thanks for the solution
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Reading properties file in classpath
 
Similar Threads
read xml from JAR file
Server Classpath in WAS6.0
Getting Resources
How to access properties file from WEB-INF/ directory
Eclipse classpath question