Get all updates of Java4s
Follow Us: Email Newsletter

How to Load Java Properties File Dynamically From the Classpath

Core Java » On Nov 25, 2012 { 7 Comments } By Sivateja

Why this properties file ? if we are using any application where there is a chance of constantly changing any credentials, something consider any java email application or any there you may need to change from,to or any properties right ? In this situation what you have to do is, opening .java file and doing the modifications and again re-compile and re-deploy.¬† Think what if you need to change 100’s of .java files :-) hmm so this .properties file.

We will place this .properties file in general at classpath location, will see how to load that properties file into .java programs and how to use it.

In this application my properties file name  = java4s.properties

Directory Structure

 

ClientLogic.javva

Output

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

7 Responses to “How to Load Java Properties File Dynamically From the Classpath”
  1. Srinivasan E says:

    We can also use following to load property file dynamically.

    prop.load(new FileInputStream(“java4s.properties”));

  2. how to print a values of map object in jsp.here map object contain key value pair.

  3. Siva says:

    Hi Sivateja,

    I tried the sample I am getting the below exception,

    Exception in thread “main” java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at org.uspto.cms.util.ClientLogic.main(ClientLogic.java:19)

    Can you pls guide me how to fix this?

    I am using Jboss Developer studio.

  4. chandra sekhar says:

    Hi sir

    what is clone() method,what are the types of clone() methods indepth explanation

  5. nagamalleswarraot says:

    i am getting
    Exception in thread “main” java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at myexample.ClientLogic.main(ClientLogic.java:19)

  6. Sankareeswaran Alagarsamy says:

    This program is not working for me.

    the below line is working fine with absolute path
    prop.load(new FileInputStream(“C:\\sankar\\workspace\\myapp\\src\\com\\myapp\\myapp.properties”));

  7. Sunil says:

    How we can write the values in that file?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.