• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading properties file in classpath

 
veda patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:\
 
veda patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
author
Saloon Keeper
Posts: 4008
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ram dhan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Jesper de Jong .

my classpath issue was resolved . thanks for the solution
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic