1

I have Java Program which access the Linux Variable(example $VARD, i have exported the variable as well), I am accessing the variable value using Runtime.getRuntime("echo $VARD") function. Issue is it prints variable name instead of its value.

Note: I am running this program in Linux server where it has JAVA JDK 1.4 version. I knew we can do that using getenv().get() function which is available from JDK 1.5..

2
  • 1
    see stackoverflow.com/questions/20610080/… Commented Dec 19, 2014 at 11:44
  • you might find what I want with System.getProperties() ? Commented Dec 19, 2014 at 11:45

1 Answer 1

1

In general you should use System.getProperty() or System.getEnv().

import java.lang.*;

public class SystemDemo {

   public static void main(String[] args) {

     // prints Java Runtime Version before property set
     System.out.print("Previous : ");
     System.out.println(System.getProperty("java.runtime.version" ));
     System.setProperty("java.runtime.version", "Java Runtime 1.6.0");

     // prints Java Runtime Version after property set
     System.out.print("New : ");
     System.out.println(System.getProperty("java.runtime.version" ));
   }
}

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

If you are using maven there is another option. You can access environment variables from maven by using ${env.VARIABLE_NAME}.

Sign up to request clarification or add additional context in comments.

1 Comment

1. Program is used to access the predefined property value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.