[ACCEPTED]-How do I pass an array or list of values to Java via system properties - and how do I access it?-environment-variables

Accepted answer
Score: 10

From Oracle Javase tutorial On the Java platform, an application uses System.getenv to retrieve environment variable values. Without an argument, getenv returns a read-only instance of java.util.Map, where the map keys are the environment variable names, and the map values are the environment variable values.

So if you have an environment variable 7 MY_LIST=val1,val2,val3, you can use it as simple as

String strlist = System.getenv().get("MY_LIST");
List<String> list = Arrays.asList(strlist.split(","));

Edit: fixed 6 call of getenv (forgot parentheses - thanks 5 to Christian)

I must precise that my anwer 4 concerns environment variable which is the title of the post. But 3 java -D MY_LIST=a,b,c ... sets system properties and it in not the same. To access system 2 properties set by -D option, I should write 1 instead :

String strlist = System.getProperty("MY_LIST");
List<String> list = Arrays.asList(strlist.split(","));

More Related questions