Context - Environment variables

<Context><Environment name="mykey" value="myvalue" type="java.lang.String"/></Context>

Object object = ((Context)(new InitialContext().lookup("java:comp/env")).lookup("mykey");.

Handling NamingException.

Public String getFromEnvironment(final String name).

{.

If(name == null) return null;.

Try.

{.

Final Object object = ((Context)(new InitialContext().lookup("java:comp/env"))).lookup(name);.

If (object != null) return object.toString();.

} catch (final Exception e) {}.

Return System.getenv(name);.

}.

JUnit test. The naming bits used by Tomcat are in a JAR file not in the TOMCAT_HOME/lib, but instead in the TOMCAT_HOME/bin/tomcat-juli.jar file!.

Updated: 2026 Aug 19