Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with System.setProperty(key,  value)

Posted on 2000-04-03
6
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 300 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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

610 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