我在linux中创建了一个独立的java应用程序。
如何获取环境变量的值(例如,在.bashrc
文件中分配)。
答案 0 :(得分:23)
要获得一个系统变量,请使用以下代码:
String sysEnvStr = System.getenv("JAVA_HOME");
如果它返回null
,则在.bashrc
文件中进行更改。尝试导出该特定变量。
答案 1 :(得分:6)
请参阅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());
}
答案 2 :(得分:1)
看看这是否适合你:
$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