Solved

Problem with System.setProperty(key,  value)

Posted on 2000-04-03
6
238 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:ziab
6 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2679502
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 :-)
0
 
LVL 3

Expert Comment

by:falter
ID: 2679578
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);
....
0
 

Expert Comment

by:erlingd
ID: 2679652
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!

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ziab
ID: 2679842
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
0
 
LVL 1

Accepted Solution

by:
sankars98 earned 100 total points
ID: 2680487


 You can use the following class to get & set the environment variable to the OS level..
 This is windows specific..

 import java.io.*;
 import java.util.*;
 public class EnvironmentVariables extends Hashtable{
   public EnvironmentVariables() {
     String[] cmd = {"cmd", "/c", "SET"};
     try {
         Process proc = Runtime.getRuntime().exec(cmd);
         BufferedReader inp = new BufferedReader(new InputStreamReader(proc.getInputStream()));
         String LINE = inp.readLine();
         while (LINE != null) {
             int eqPos = LINE.indexOf("=");
             if (eqPos >= 0) {
               String name = LINE.substring(0, eqPos);
               String value = LINE.substring(eqPos + 1);
               super.put(name.toUpperCase(), value);
             }
             LINE = inp.readLine();
         }
         inp.close();
       } catch (IOException e) {
         System.out.println("Weird I/O error");
     }
   }
   public synchronized String get(Object name) {
     String theName = name.toString().toUpperCase();
     return (String)super.get(theName);
   }
   public synchronized Object put(Object name, Object value) {
       String theName = name.toString().toUpperCase();
       String theValue = value.toString();
       Object result = super.get(theName);
       // this is windows specific
       String[] cmd = {"cmd", "/c", "SET " + theName + "=" + theValue};
       try {
         Process proc = Runtime.getRuntime().exec(cmd);
         BufferedReader inp = new BufferedReader(new InputStreamReader(proc.getInputStream()));
         String LINE = inp.readLine();
         while (LINE != null) {}
           super.put(name.toString().toUpperCase(), value.toString());
           return result;
       } catch (IOException e) {
         System.out.println("Weird I/O error");
         return null;
       }
   }
 }



 Usage :

 EnvironmentVariables eVariables = new EnvironmentVariables();
 String value = eVariables.get("MYENVIRONMENTVARIABLE");


Sankar S.
0
 

Author Comment

by:ziab
ID: 2689872
Thanks Sankar S.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 99
print map entry 34 69
Spring Framework HTTPSession management 1 36
spring jars download 1 27
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question