why System.getenv(“CATALINA_HOME”) returns null?

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:

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.

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().

    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:Userschorusheng
ProgramData:C:ProgramData
PATHEXT:.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
JAVA_HOME:C:Program FilesJavajdk1.6.0_20
ProgramFiles(x86):C:Program Files (x86)
TEMP:C:UsersCHORUS~1AppDataLocalTemp
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 CorporationPhysXCommon;d:Program Files (x86)KOUTONCTBS Standard Client;C:Program Files (x86)PC Connectivity Solution;D:oracleproduct11.2.0dbhome_1bin;C:Program Files (x86)Common FilesNetSarang;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program FilesIntelWiFibin;C:Program FilesCommon FilesIntelWirelessCommon;C:Program Files (x86)EgisTec BioExcess;C:Program Files (x86)EgisTec BioExcessx64;C:Program Files (x86)Common FilesThunder NetworkKanKanCodecs;d:Program Files (x86)TencentQQPCMgr6.6.2150.401;D:Program Files (x86)TortoiseSVNbin;d:Program Files (x86)DigiWin OpenVPNbin;d:Program Files (x86)TencentQQPCMgr6.6.2150.401;F:chegaeclipsej2ee3.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:UsersCHORUS~1AppDataLocalTemp
CommonProgramFiles:C:Program FilesCommon Files
CLASSPATH:.;C:Program FilesJavajdk1.6.0_20libdt.jar;C:Program FilesJavajdk1.6.0_20libtools.jar;
LOGONSERVER:\CHORUS
PROCESSOR_ARCHITECTURE:AMD64
FP_NO_HOST_CHECK:NO
OS:Windows_NT
HOMEPATH:Userschorusheng
PROCESSOR_LEVEL:6
CommonProgramW6432:C:Program FilesCommon Files
1830B7BD-F7A3-4c4d-989B-C004DE465EDE:f44:431b280
LOCALAPPDATA:C:UserschorushengAppDataLocal
COMPUTERNAME:CHORUS
windir:C:Windows
SystemRoot:C:Windows
asl.log:Destination=file
NUMBER_OF_PROCESSORS:4
USERNAME:chorusheng
PUBLIC:C:UsersPublic
PSModulePath:C:Windowssystem32WindowsPowerShellv1.0Modules
CommonProgramFiles(x86):C:Program Files (x86)Common Files
ComSpec:C:Windowssystem32cmd.exe
APPDATA:C:UserschorushengAppDataRoaming
null

as we can see, the last line is null which is the value of CATALINA_HOME variable.

PS: my tomcat is not an installation edition.

Answer

Possible reasons:

  1. 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. Use My Computer > Advanced > Environment Variables to make a variable visible to all new processes.

  2. The process which tries to read the variable is already running. Restart it.

    Note: That could be the IDE if you start Tomcat from the debugger or the standalone Tomcat process when you start it from the command line.

  3. The start script of Tomcat unsets the variable before it invokes java.exe

  4. Tomcat unsets the variable in it’s Java code.

Leave a Reply

Your email address will not be published. Required fields are marked *