Solved

Problem with System.setProperty(key,  value)

Posted on 2000-04-03
6
231 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
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 …
This video teaches viewers about errors in exception handling.

762 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

18 Experts available now in Live!

Get 1:1 Help Now