Solved

Problem with System.setProperty(key,  value)

Posted on 2000-04-03
6
241 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 74
how to install java on RHEL image on EC2 4 58
java example issue 5 46
DTD and JAVA versions 1 33
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

730 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