File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reading properties file in classpath" Watch "Reading properties file in classpath" New topic

Reading properties file in classpath

veda patil

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

private static String fileName = "";

I have set classpath variable to :
classpath = D:\

But getting error.

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

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: 15081

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

classpath = D:\

use this:

classpath = D:\

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
veda patil

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

Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 692
> 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: 3753

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.

[OCA 8 Book] [Blog]
ram dhan

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

my classpath issue was resolved . thanks for the solution
I agree. Here's the link:
subject: Reading properties file in classpath
It's not a secret anymore!