开发者

How to I obtain the value of the environment variables? [duplicate]

开发者 https://www.devze.com 2023-02-08 22:37 出处:网络
This question already has answers here: 开发者_Go百科 Closed 12 years ago. Possible Duplicate: Access shell environment variables Java
This question already has answers here: 开发者_Go百科 Closed 12 years ago.

Possible Duplicate:

Access shell environment variables Java

I've created a stand-alone java application in linux.

How can I obtain the value of the environment variables (e.g. assigned in the .bashrc file).


For obtaining only one System Variable use the following code:

 String sysEnvStr = System.getenv("JAVA_HOME");

If it returns null then make changes in your .bashrc file. Try exporting that particular variable.


See this howto:

// just one
System.out.println("PATH = " + System.getenv("PATH"));

// all of them
Map env = System.getenv();
for (Iterator it=env.entrySet().iterator(); it.hasNext(); ) {
   Map.Entry entry = (Map.Entry)it.next();
   System.out.println(entry.getKey() + " = " + entry.getValue());
}


See if this works for you:

$cat Env.java 
class Env { 
  public static void main( String ... args ) {
    for( Object o : System.getenv().entrySet()  ){
      System.out.println( o );
    }
  }
}
$java Env
TERM=xterm
LESSCLOSE=/usr/bin/lesspipe %s %s
SESSION_MANAGER=local/oreyes-laptop:@/tmp/.ICE-unix/2341,unix/oreyes-laptop:/tmp/.ICE-unix/2341
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GDMSESSION=gnome
XDG_SESSION_COOKIE=2604d05dc122db9b636d41744aeeb7a2-1296882161.129722-1315567412
MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path
PS1=$
NLSPATH=/usr/dt/lib/nls/msg/%L/%N.cat
GDM_KEYBOARD_LAYOUT=latam
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg
XAUTHORITY=/var/run/gdm/auth-for-oreyes-5QyKYh/database
GDM_LANG=en_US.utf8
USERNAME=oreyes
GTK_MODULES=canberra-gtk-module
GNOME_KEYRING_CONTROL=/tmp/keyring-DqJ9Sg
SHLVL=1
SPEECHD_PORT=7560
XFILESEARCHPATH=/usr/dt/app-defaults/%L/Dt
COLORTERM=gnome-terminal
XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/
JDK_HOME=/usr/lib/jvm/java-6-sun
LOGNAME=oreyes
WINDOWID=79691780
GPG_AGENT_INFO=/tmp/seahorse-vkmfjC/S.gpg-agent:2385:1
SSH_AUTH_SOCK=/tmp/keyring-DqJ9Sg/ssh
LD_LIBRARY_PATH=/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/i386/client:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/i386:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/../lib/i386
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-15CfDJRFNg,guid=6ebfc2e2421576b21ec0d2284d4cd9f2
SHELL=/bin/bash
GNOME_KEYRING_PID=2323
GOARCH=386
DESKTOP_SESSION=gnome
DISPLAY=:0.0
USER=oreyes
HOME=/home/oreyes
HISTCONTROL=ignoreboth
LESSOPEN=| /usr/bin/lesspipe %s
DEFAULTS_PATH=/usr/share/gconf/gnome.default.path
0

精彩评论

暂无评论...
验证码 换一张
取 消