Blayter.com

Reading environment variables on the server via ColdFusion

I had the need to read in a environment variable off of the server via ColdFusion. Just replace the ENVIRONMENT_VARIABLE_ON_THE_SERVER with your own variable that you are going after.

<cfset system = CreateObject("java", "java.lang.System")>
<cfset environment = system.getenv()>
<cfset env = environment.get("ENVIRONMENT_VARIABLE_ON_THE_SERVER")>
<cfoutput>#env#</cfoutput>

This is based on some java code that I wrote up for a LCDS project.

public static String getEnvironment(String environmentVariableName) {
Map<String, String> env = System.getenv();
return env.get(environmentVariableName);
}

Comments
That's awesome. I was just cruising some very old code I wrote that does essentially the same thing via .NET, i think I was calling it with cfexecute as a command-line exe. Stupid of me to not think of Java at the time. Thanks.
# Posted By Nathan Strutz | 1/23/12 3:48 PM
On Railo anyhow...this picks up the environment as set in Railo config files but does NOT pick up variable exported on the underlying OS.
# Posted By Bill Ryder | 7/27/12 12:54 PM
I went through and was able to run this without a problem on Railo to export the path variable

<cfset system = CreateObject("java", "java.lang.System")>
<cfset environment = system.getenv()>
<cfset env = environment.get("PATH")>
<cfoutput>#env#</cfoutput>
# Posted By John Blayter | 12/13/12 4:49 PM