Friday, August 29, 2008

CF8 Tip: Determine path to cmd.exe programmatically

So I was curious if the path to the windows command line interpreter (cmd.exe) could be obtained programatically. I discovered it can with ColdFusion 8. The java.lang.System class contains a method called getenv that can return the value of a specific environment variable. From what I have read, most windows versions store the path to the command line interpreter in a variable named COMSPEC. So I was able to display the path using this snippet:

<cfset pathToExe = createObject("java", "java.lang.System").getEnv("COMSPEC")>
<cfdump var="Path = #pathToExe#">

.. and display all environment variables using:

<cfset environ = createObject("java", "java.lang.System").getEnv()>
<cfdump var="#environ#">

Unfortunately it does not work with MX 7. MX 7 uses and older jvm (version 1.4), which does not contain one of the getenv() methods and the other is marked as deprecated. Fortunately, getenv() was resurrected in time for jvm 1.6, making it easy to read windows environment variables under ColdFusion 8.

0 comments:

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Header image adapted from atomicjeep