System Command

PDF HTML FlashPaper

Main

System Command Example

Suppose we want to execute the command to determine the version of Java running on your system. You may execute the following command:

java -version

Which on my machine results in:

java version "1.6.0_03" Java(TM) SE Runtime Environment (build 1.6.0_03-b05) Java HotSpot(TM) Client VM (build 1.6.0_03-b05, mixed mode)

Example Usage

The system command object is created using the command:

<cfset syscmd = createObject("java","au.com.webcode.util.SystemCommand").init()>

This object has one function: execute(command,timeout).

<cfset result = syscmd.execute(command,timeout)>

The parameters are:

  • command - The full command string to execute.
  • timeout - (Optional) A timeout in milliseconds. The process will be terminated when the timeout is reached and an Exception will be thrown. The default timeout is 10 seconds.

Return Value

The value returned from the execute() function is an object with the following functions:

  • getCommand() - The original command that was executed.
  • getStandardOutput() - The standard output produced by the command.
  • getErrorOutput() - The error output produced by the command.
  • getExitValue() - An integer value provided by the process that was executed. A value of zero usually indicates that all was fine. A non zero value usually indicates a problem occurred.

Example

A simple example of ColdFusion code to use the system command utility.

<cfset command = "java -version"> <cfset syscmd = createObject("java","au.com.webcode.util.SystemCommand").init()> <cfset result = syscmd.execute(command)> <cfoutput> Command: #result.getCommand()#<br /> ExitValue: #result.getExitValue()#<br /> Error Output: #result.getErrorOutput()#<br /> Standard Output: #result.getStandardOutput()#<br /> </cfoutput>