1. System.getenv()
returns the system environment variable value, the example is as follows:
- {PAPERSIZE=a4, TERM=xterm, XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1, JAVA_HOME=/home/dsl/programs/jdk1.6.0_45, S_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:, LC_MEASUREMENT=zh_CN.UTF-8, XDG_RUNTIME_DIR=/run/user/1000, LANG=zh_CN.UTF-8} rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:, LC_MEASUREMENT=zh_CN.UTF-8, XDG_RUNTIME_DIR=/run/user/1000, LANG=zh_CN.UTF-8} rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:, LC_MEASUREMENT=zh_CN.UTF-8, XDG_RUNTIME_DIR=/run/user/1000, LANG=zh_CN.UTF-8} LC_MEASUREMENT=zh_CN.UTF-8, XDG_RUNTIME_DIR=/run/user/1000, LANG=zh_CN.UTF-8} LC_MEASUREMENT=zh_CN.UTF-8, XDG_RUNTIME_DIR=/run/user/1000, LANG=zh_CN.UTF-8}
There are several common ways to set system environment variables:
1.1. The ".bashrc" file
in the home directory of the logged-in user can set the system environment variables in the ".bashrc" file under the home directory of the current logged-in user.
1.2. Set
in the Shell Execute the following commands in order in the Shell to set the environment variable "dslztx":
- dslztx="This is a newbie."
- export dslztx
Now there is the following Java code:
- public class Main {
- publicstaticvoid main(String[] args) {
- System.out.println(System.getenv("dslztx"));
- }
- }
Execute "javac Main.java;java Main" in sequence in the same shell to get the print result of "This is a newbie.".
1.3. Setting in IDE
If you use "Intellij Idea", you can set environment variables in the "Environment variables" option of the "Run-->Edit Configurations" window.
As shown in Figure 1.
figure 1
二、System.getProperties()
Returns the Java process variable value, an example is as follows:
- {java.runtime.name=Java(TM) SE Runtime Environment, os.name=Linux, sun.jnu.encoding=UTF-8, java.library.path=/home/dsl/programs/jdk1.6.0_45/jre/lib/i386/client:/home/dsl/programs/jdk1.6.0_45/jre/lib/i386:/home/dsl/programs/jdk1.6.0_45/jre/../lib/i386:/home/dsl/programs/jdk1.6.0_45/jre/lib/i386/server:/home/dsl/programs/jdk1.6.0_45/jre/lib/i386:/home/dsl/programs/jdk1.6.0_45/jre/../lib/i386:/mnt/bigdisk/programs/idea-IU-141.1532.4/bin::/usr/java/packages/lib/i386:/lib:/usr/lib, java.specification.name=Java Platform API Specification, java.class.version=50.0, sun.management.compiler=HotSpot Client Compiler, os.version=3.16.0-30-generic, user.home=/home/dsl, user.timezone=, java.awt.printerjob=sun.print.PSPrinterJob, idea.launcher.bin.path=/mnt/bigdisk/programs/idea-IU-141.1532.4/bin, file.encoding=UTF-8}
常见的有以下几种途径可以设置Java进程变量:
2.1、通过命令行参数的"-D"选项
现在有如下代码:
- public class Main {
- public static void main(String[] args) {
- System.out.println(System.getProperty("dslztx"));
- }
- }
依次执行如下命令:
- javac Main.java
- java -Ddslztx="This is a newbie!" Main
可以得到"This is a newbie!"的打印结果。
2.2、进程运行中动态设置
代码例子如下:
- public class Main {
- public static void main(String[] args) {
- System.setProperty("dslztx", "This is a newbie!");
- System.out.println(System.getProperty("dslztx"));
- }
- }
Run to get the print result of "This is a newbie!".
2.3. Setting in IDE
If you use "Intellij Idea", you can set the Java process variable in the "VM options" option of the "Run-->Edit Configurations" window.
as shown in picture 2.
figure 2