简体   繁体   中英

String[] value from Environment Variable

Could anyone help me how to get values from environment variable?

String[] extensions = {"xml", "java", "dat"};

Currently i am passing xml, java and dat files. Now i would like to get these values from environment variable.

I tried this:

String[] extensions = {System.getenv("LIST")};

But i get null value each time.

System.getenv() will return a Map. from where you can iterate the map and put in an array.

Map<String, String> env = System.getenv();
// allocate an array with env.size()
for (String envName : env.keySet()) {
    // add env.get(envName) to array.
}

For PATH and CLASSPATH variables, you need to discover the conventional separator for them.

PATH

String path = System.getEnv("PATH");
// See http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
String[] files = path.split(System.getProperty("path.separator"));
List<String> unescapedFiles = new ArrayList<>();
for (String file: files) {
    // Exercise for the reader
    String unquotedFile = ...;
    String javaSlashedFile = ...;
    unescapedFiles.add(javaSlashedFile);
}
return unescapedFiles;

The loop should take file names like "C:\\Program Files\\perl\\perl.exe" and convert them to C:/Program Files/perl/perl.exe .

CLASSPATH

There is a more reliable alternative for the class path. Also listed on the same Java Tutorial page is the system property "java.class.path". This is more reliable than the environment variable because it takes into account java -cp *path* invocations and JAR files with Class-Path manifests. So, replace the line

String path = System.getEnv("PATH");

with

String classPath = System.getProperty("java.class.path");

The value returned for an individual environment variable will always be a string. In your case, the string may look like this: "xml:java:dat". Once you retrieve this value (try System.getenv("DESIRED_ENVIRONMENT_VARIABLE_NAME_HERE") ) you will need to parse it into individual values. Try using the String.split method (perhaps stringVariableName.split(":") ).

Once split, you will have the values in a String array (for example: String[] values ).

For Java to get an environment variable, there must be a an environment variable for it to get. As they come from the environment, how you set up the environment variables is environment (operating system) dependent.

You do not indicate which operating system you are using. If the environment is a POSIX exec to start the JVM, one of the exec functions can be given a set of values for the environment values.

For Unix shells you can set values in the statement that executes the JVM:

 NAME=value java ...

or export them in the shell script before executing the JVM:

 NAME=value
 export NAME
 java ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM