Link to home
Start Free TrialLog in
Avatar of ziab
ziab

asked on

Problem with System.setProperty(key, value)

Instead of "System.getEnv()", which is deprecated since java 1.2,  we use the method "System.getProperty(Key ) for the reading of environment- variables.
With the method "System.setProperty(key, value)" we set a selfdefined environment-variable.

When we start another Programm which read the selfdefined environment-variable with the method "System.getProperty(Key )" we get returnvalue "null".

Which Lifetime has a Property set by the "System.setProperty(key, value)" method?

Ziab
Avatar of Sasha_Mapa
Sasha_Mapa

The System class has a static object named props where all the properties are kept. On initialization, System has a native method initProperties which it calls to fill in the properties in props. The setProperty method adds a property to the props object.
All this means that the properties will be shared between applications that run on the same instance of a JVM. Programs running on different instaces will not share properties (because the setProperty method does nothing to change any external objects in any way).
I am not 100% sure about this, as I never worked with properties, just looked at the docs and source code for System so if other experts can correct/confirm my answer, please do :-)
If you wanna make the new defined key available to an other process (second JVM), try to save your properties in the first process and load them in the second one.

Have a look at java.utils.Properties class and something like

....
Properties myProps = System.getProperties();
myProps.put(key,value);
myProps.save("myPropertyFile");
....


In second program do something like
....
Properties myProps = Properties.load("myPropertyFileName);
System.setProperties(myProps);
....
I think the only way to change env variables for an "upcomming" program is to change the env array you use in "java.lang.Runtime.exec".
As far as I can remember env. variables are static they are parameters when you start the program up. And can only be changed within a batch/command script.
Pls. correct me if I'm wrong!

Avatar of ziab

ASKER

Thanks for the comments,

but we are seaching for a possibility to set, save and hold a selfdefined env-var in the env only with Java.

Ziab
ASKER CERTIFIED SOLUTION
Avatar of sankars98
sankars98

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ziab

ASKER

Thanks Sankar S.