Java 为什么 System.getenv("CATALINA_HOME") 返回 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16413573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why System.getenv("CATALINA_HOME") returns null?
提问by hiway
I define a system variable CATALINA_HOME
which refers to tomcat installation directory in my OS system(windows 7 ultimate 64 bit), now I want to get it by java, my code is below:
我定义了一个系统变量CATALINA_HOME
,它指的是我的操作系统系统(windows 7 Ultimate 64位)中的tomcat安装目录,现在我想通过java获取它,我的代码如下:
System.out.println(System.getenv("CATALINA_HOME"));
it returns null, I am pretty sure this variable exists in my system, I type set catalina_home
in cmd console, it shows exactly the value assigned to it.
它返回 null,我很确定这个变量存在于我的系统中,我set catalina_home
在 cmd 控制台中输入,它准确地显示了分配给它的值。
So why can't I get it, or is there other way to get system env variable?
那么为什么我不能得到它,或者有其他方法来获取系统环境变量吗?
PS: the below are all variables retrieved by System.getenv()
.
PS:以下是System.getenv()
.
Map<String, String> env = System.getenv();
for (String key : env.keySet())
{
System.out.println(key + ":" + env.get(key));
}
System.out.println(System.getenv("CATALINA_HOME"));
output:
输出:
USERPROFILE:C:\Users\chorusheng
ProgramData:C:\ProgramData
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
JAVA_HOME:C:\Program Files\Java\jdk1.6.0_20
ProgramFiles(x86):C:\Program Files (x86)
TEMP:C:\Users\CHORUS~1\AppData\Local\Temp
SystemDrive:C:
ProgramFiles:C:\Program Files
Path:C:/Program Files (x86)/Java/jre7/bin/client;C:/Program Files (x86)/Java/jre7/bin;C:/Program Files (x86)/Java/jre7/lib/i386;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;d:\Program Files (x86)\KOUTON\CTBS Standard Client;C:\Program Files (x86)\PC Connectivity Solution\;D:\oracle\product.2.0\dbhome_1\bin;C:\Program Files (x86)\Common Files\NetSarang;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\EgisTec BioExcess\;C:\Program Files (x86)\EgisTec BioExcess\x64;C:\Program Files (x86)\Common Files\Thunder Network\KanKan\Codecs;d:\Program Files (x86)\Tencent\QQPCMgr.6.2150.401;D:\Program Files (x86)\TortoiseSVN\bin;d:\Program Files (x86)\DigiWin OpenVPN\bin;d:\Program Files (x86)\Tencent\QQPCMgr.6.2150.401;F:\chega\eclipsej2ee3.7;
HOMEDRIVE:C:
DYNA_HOME:E:\plm
PROCESSOR_REVISION:2502
USERDOMAIN:chorus
ALLUSERSPROFILE:C:\ProgramData
ProgramW6432:C:\Program Files
PROCESSOR_IDENTIFIER:Intel64 Family 6 Model 37 Stepping 2, GenuineIntel
SESSIONNAME:Console
TMP:C:\Users\CHORUS~1\AppData\Local\Temp
CommonProgramFiles:C:\Program Files\Common Files
CLASSPATH:.;C:\Program Files\Java\jdk1.6.0_20\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_20\lib\tools.jar;
LOGONSERVER:\CHORUS
PROCESSOR_ARCHITECTURE:AMD64
FP_NO_HOST_CHECK:NO
OS:Windows_NT
HOMEPATH:\Users\chorusheng
PROCESSOR_LEVEL:6
CommonProgramW6432:C:\Program Files\Common Files
1830B7BD-F7A3-4c4d-989B-C004DE465EDE:f44:431b280
LOCALAPPDATA:C:\Users\chorusheng\AppData\Local
COMPUTERNAME:CHORUS
windir:C:\Windows
SystemRoot:C:\Windows
asl.log:Destination=file
NUMBER_OF_PROCESSORS:4
USERNAME:chorusheng
PUBLIC:C:\Users\Public
PSModulePath:C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
CommonProgramFiles(x86):C:\Program Files (x86)\Common Files
ComSpec:C:\Windows\system32\cmd.exe
APPDATA:C:\Users\chorusheng\AppData\Roaming
null
as we can see, the last line is null which is the value of CATALINA_HOME variable.
正如我们所看到的,最后一行是空的,它是 CATALINA_HOME 变量的值。
PS: my tomcat is not an installation edition.
PS:我的tomcat不是安装版。
采纳答案by Aaron Digulla
Possible reasons:
可能的原因:
You've used
set CATALINA_HOME
in a command prompt. That makes this variable local to this window. It should be visible to processes started from this command prompt but nowhere else. UseMy Computer > Advanced > Environment Variables
to make a variable visible to all new processes.The process which tries to read the variable is already running. Restart it.
The start script of Tomcat unsets the variable before it invokes
java.exe
Tomcat unsets the variable in it's Java code.
您已经
set CATALINA_HOME
在命令提示符中使用过。这使得这个变量本地化到这个窗口。它应该对从此命令提示符启动的进程可见,但在其他任何地方都不可见。使用My Computer > Advanced > Environment Variables
使变量可见于所有新进程。尝试读取变量的进程已经在运行。重新启动它。
Tomcat 的启动脚本会在调用之前取消设置变量
java.exe
Tomcat 在它的 Java 代码中取消设置变量。
回答by Mostafa Anbarmoo
If method system.getenv("some name")
return null
probably you define this variable in your environment after lunching your Eclipse IDE. Just restart your Eclipse and then run your project again.
如果方法system.getenv("some name")
返回,null
您可能在 Eclipse IDE 吃完午饭后在您的环境中定义了这个变量。只需重新启动 Eclipse,然后再次运行您的项目。