Search code examples
javapropertiesjndi

How to point to jdni.properties file to set properties in Java


I can use System.getProperties() method to set properties in Java.

System.getProperties().put("java.naming.factory.initial",
 "fr.dyade.aaa.jndi2.client.NamingContextFactory"); 
System.getProperties().put("java.naming.factory.host", "localhost");
System.getProperties().put("java.naming.factory.port", "16400");

How can I get the same effect by reading the properties stored in a file?

When I have a jdni.properties with the following content:

java.naming.factory.initial  fr.dyade.aaa.jndi2.client.NamingContextFactory
java.naming.factory.host localhost
java.naming.factory.port 16400

How can I teach Java to read them as properties? I use eclipse for development.


Solution

  • The jdni.properties should be in the class path. In eclipse, I had to use Advanced tab in Run Configuration/CLASSPATH to add the directory to be included as class path.

    enter image description here