CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 1999
    Posts
    3

    System environment variables

    Hi,

    I would like to get the value of some system environment variables, but can't get it using the java.lang.System class (method "public static String getProperty(String key)"). This class just allow me to get the command line parameters passed to the JVM.

    How can I get the system environment variables?

    Thanks for any suggestion.


  2. #2
    Join Date
    May 1999
    Location
    USA
    Posts
    27

    Re: System environment variables

    Did you check the other two fucntions in that class? Viz., getProperties() and getProperty(String key, String def)? getProperties() returns an instance of "Properties" and contains all the system parameters like java.version, OS name, version etc etc.
    Please let me know if that's suitable..
    Thanks,
    Amaresh


  3. #3
    Join Date
    Sep 1999
    Posts
    3

    Re: System environment variables

    It makes no difference doing System.getProperties().getProperty("MY_ENV_VAR") or System.getProperty("MY_ENV_VAR"). In addition, System.getProperty(String key, String def) just initialize the value associatet to key if it isn't defined.
    The class java.lang.System just allow to get the command line parameters passed to the JVM and some JVM defined environment variables "like java.version, OS name, version etc etc.".



  4. #4
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: System environment variables


    There is not pure java solution to get system dependent variables. you have to write
    native code for this. I dont know about JNI. There might be some solution in there.


  5. #5
    Join Date
    Nov 1999
    Posts
    10

    Re: System environment variables

    Hi,

    I have the same need to get the system variable value which is not system property.
    please let me know if you the solution .
    question:



    Hi,

    I am trying to get the environment variavle value
    (ex: variable : xx
    value : trytyy
    which is set manually in Start/control panel/system/environment/System variables in NT machine.How to get this value in servlet.

    ________________________________
    my environment is
    jdk1.2
    javawebserver1.1.3
    NT osystem
    jsdk2.0
    ________________________________


    I found the getenv(String) function but its deprecated function top of that its not working (error in run time : server returned a invalid and unrecognized response.............).

    I tried getPropert(),getProperties()................but no way.


    Thanks




  6. #6
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: System environment variables


    Sorry for my misleading answer in the other post..
    You can use the following class to get the environment variable..
    This is for 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");





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured