Is there a way to pass a list of values into Java, using only one system property?
I'm thinking of something along the lines -DMY_LIST=val1,val2,val3
or -DMY_LIST={val1, val2}
Any ideas?
How would I access that in Java?
Update: I initially asked for environment variables, but actually meant system properties. Similar principle, but quite different... I changed title and text accordingly now. Thanks, @serge-ballesta
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 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 call of getenv (forgot parentheses - thanks to Christian)
I must precise that my anwer concerns environment variable which is the title of the post. But java -D MY_LIST=a,b,c ...
sets system properties and it in not the same. To access system properties set by -D
option, I should write instead :
String strlist = System.getProperty("MY_LIST");
List<String> list = Arrays.asList(strlist.split(","));
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.