• Post Reply Bookmark Topic Watch Topic
  • New Topic

set env property for the project  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Folks,

Please help me to set the env variable for the whole project.

Some thing like project contains 20 to 30 class files located accross the packages. The variables set should be visible to all the class files.
I tried with:

java.util.Properties prop = new java.util.Properties();
prop.setProperty("one", "NULL");
prop.setProperty("two", "welcome");
prop.setProperty("three", "hello");
System.setProperties(prop);

How to make it visible to all the classes.

Regards,
Arjun
 
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place your peoperties in a props file and use methods of ClassLoader or Class to load the properties file on startup.

Read this excellent article to find out more about loading properties.

Thanks,
Ram.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Part of the issue here is where to declare the Properties object. If you declare it local within a particular method, then no one else will be able to access it. Perhaps there is a design pattern that will help solve this problem. Using a Singleton class to encapsulate the project's properties is one idea that comes to mind. If you are unfamiliar with the Singleton design pattern, you should google for it.

Layne
 
Arjun K
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne,

Thats a good idea. I do know about the singleton design pattern; which gives same instance on every object creation.

Let me know if there is any thing interesting....

-Arjun.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!