Newsletter |
How to Load Java Properties File Dynamically From the Classpath
Core Java » on Nov 25, 2012 { 8 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
package java4s; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Properties; public class ClientLogic { public static void main(String... args) { // First Way Properties prop = new Properties(); InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("java4s.properties"); try{ prop.load(inputStream); }catch(IOException e) { e.printStackTrace(); } System.out.println("------- By Using Thread class -------"); //Print required values System.out.println(prop.get("user")); //Print all the properties System.out.println(prop); // Second Way URL url = ClassLoader.getSystemResource("java4s.properties"); try{ prop.load(url.openStream()); }catch(Exception e) { e.printStackTrace(); } System.out.println("------- By Using URL class -------"); System.out.println(prop); } }
Output
β ββ
You Might Also Like
::. About the Author .:: | ||
Comments
8 Responses to “How to Load Java Properties File Dynamically From the Classpath”
We can also use following to load property file dynamically.
prop.load(new FileInputStream(“java4s.properties”));
how to print a values of map object in jsp.here map object contain key value pair.
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.
Hi sir
what is clone() method,what are the types of clone() methods indepth explanation
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)
You have to create your own property file like(java.properties) and write anything you want on that file,
Save and re-compile it.
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”));
How we can write the values in that file?