Tomcat 9.0.45 环境变量配置后System.getProperty获取值抛出空指针异常问题咨询
Hey there, let's break down why your System.getProperty(envVarName) call is returning null even though you set JAVA_OPTS in Tomcat's batch files.
The Core Issue
When you debug Tomcat through Eclipse, Eclipse doesn't use the catalina.bat or startup.bat files you modified. Instead, Eclipse manages its own separate launch configuration for the Tomcat server, so any JVM arguments you added to those batch files won't be picked up during debugging sessions. That's why System.getProperty("APP_ROOT_DIR") is coming back null and causing the NPE.
How to Fix It
You need to add the JVM argument directly in Eclipse's Tomcat launch configuration:
- Open the Servers view in Eclipse (if you don't see it, go to
Window > Show View > Servers). - Right-click on your Tomcat server instance and select Open.
- In the server configuration page, look for and click Open Launch Configuration (this might be labeled differently in some Eclipse versions, like just "Launch Configuration").
- Switch to the Arguments tab.
- In the VM arguments text box, add your JVM property:
-DAPP_ROOT_DIR=C:\opt\app - Click Apply then Close, and restart your Tomcat server in debug mode.
Quick Verification
To make sure it's working, add a quick debug print statement before the line that throws the NPE:
System.out.println("APP_ROOT_DIR value: " + System.getProperty(envVarName));
This will confirm if the property is being loaded correctly before proceeding with your setStaticDirName call.
Remember, if you ever start Tomcat directly via the batch files outside of Eclipse, your original JAVA_OPTS setup will work fine—this fix is only needed for Eclipse-integrated debugging.
内容的提问来源于stack exchange,提问作者hell_storm2004

