Solved

Problem with System.setProperty(key,  value)

Posted on 2000-04-03
6
232 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
network + 7 80
Responding to Java JComponent extended classes's resize event 4 52
Java / Linux and Regular Expressions 11 70
rhino JavaScript import, load 25 67
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now