To open the environment variables in the string?

As in Java to convert a string type

"Tralala ~/bin;${JAVA_HOME} trululu"

in

"Tralala /home/lenochka/bin;/bin/java trululu"

— i.e. expand all the environment variables and a tilde, which is inside the string.


Googled — needed library is not found :(
October 10th 19 at 15:29
4 answers
October 10th 19 at 15:31
The task is divided into 3 parts

1) the allocation of variable names
2) receive their values
3) Assembly line

For 1, you can use regular expressions, or any template-engine (expensive).
2 you can use the System.getenv
3 — again using regular expressions or simple replacement with a String.replace
October 10th 19 at 15:33
You can use System.getProperty(String Property);
Only need for all variables to do a mapping of the parameter of the function. For example:
'JAVA_HOME': 'java.home',
'~': 'user.home'

Java just started learning, so I don't know, maybe there is a more correct version
nuuu, so clear, but it from the string need to isolate the variables, and then, once the value — back line to collect. want easier - vesta.Mann28 commented on October 10th 19 at 15:36
October 10th 19 at 15:35
try to do System.exec("$JAVA_HOME").getOutputStream() and string count. Get what the variable contains
or "echo $JAVA_HOME" - vesta.Mann28 commented on October 10th 19 at 15:38
October 10th 19 at 15:37
Map<string, string> env = System.getenv();
 for (String envName : env.keySet()) {
 System.out.format("%s=%s%n", envName, env.get(envName));
 }</string>

->
JAVA_HOME=/usr/lib/jvm/java-8-oracle
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg
XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
GNOME_KEYRING_CONTROL=
LANG=en_us.UTF-8
XDG_SESSION_TYPE=x11
XDG_SESSION_ID=c1
DISPLAY=:0
GDM_LANG=en_us
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/quadra
UPSTART_EVENTS=started xsession
SESSION=ubuntu
DESKTOP_SESSION=ubuntu
GPG_AGENT_INFO=/run/user/1000/keyring/gpg:0:1
...

Find more questions by tags Java