1

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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
kino
  • 11
  • 2
  • 6

4 Answers4

3

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.
}
stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Thanks Stine..I am trying to replicate http://www.kodejava.org/examples/359.html but i get error here: Collection files = FileUtils.listFiles(root, env, recursive); – kino Jun 14 '13 at 16:50
2

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");
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0

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).

DwB
  • 37,124
  • 11
  • 56
  • 82
0

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 ...
Raedwald
  • 46,613
  • 43
  • 151
  • 237