开发者

How do i get the value of a SHELL environment variable in java

开发者 https://www.devze.com 2023-03-21 07:54 出处:网络
on linux command line: export DB_SYSTEM=mysql echo $DB_SYSTEM result = mysql Within Java , i want to access and print value of DB_SYSTEM

on linux command line:

  1. export DB_SYSTEM=mysql
  2. echo $DB_SYSTEM result = mysql
  3. Within Java , i want to access and print value of DB_SYSTEM

could it be:

public Proper开发者_Go百科ties getEnvironment() throws java.io.IOException {
    Properties env = new Properties();
    env.load(Runtime.getRuntime().exec("env").getInputStream());
    return env;
    }

Properties env = getEnvironment();
String myEnvVar = env.get("DB_SYSTEM");


Just use System.getenv:

String dbSystem = System.getenv("DB_SYSTEM");

If you call it with no arguments, it returns all the environment variables:

Map<String, String> env = System.getenv();
String dbSystem = env.get("DB_SYSTEM");
0

精彩评论

暂无评论...
验证码 换一张
取 消