The EXEC command submits a quoted command string or a string variable to the underlying operating system (such as Windows, MacOS, Linux, etc.) to be executed in a separate process. The specific command string's syntax depends on which operating system you are using. The command string may be followed by any number of optional argument strings, also enclosed in quotes, or optional argument string variables.
Alternatively, the single command string may include all the needed arguments within it, but depending on the presence of blanks between arguments and within file paths, the execution might fail. That's because a single command string is parsed assuming spaces as the delimiter. Sometimes that is not what you intend. If using a single command string that includes all needed arguments does not work, then break it into several strings, the first being the complete directory path of the program or batch file to run, and the others being the arguments of that program or batch file.
By default, your Resampling Stats program will wait at the EXEC command line until the process that is executing the command string completes. If you want the your Resampling Stats program to proceed independently without waiting for the result, use the noWait keyword.
By default, any output from a batch file will run without generating any output to the Output Window. While you are debugging your batch files you can use the verbose keyword to route all output from the executing batch file to the Output Window. Each line of output will be preceded by the line number of the EXEC command followed by either "OUT> " or "ERR> " depending on whether the output is from the standard output or the error output, respectively.
By default the process created by the operating system will inherit the working directory of the Statistics101 program. You can use the dir keyword to change the working directory. Dir sets the directory that the target program will look in to find any files listed as arguments. Dir does not set the directory in which the operating system process will look for the target of the command string—the command string should give the full path of the program or batch file to be run. You will minimize debugging your arguments to the EXEC command if you use full path names for the command string and all its arguments.
Alternatively, you can create a batch file that sets the working directory and executes your desired command(s). Then you can use the EXEC command to invoke the batch file. Note that the batch file will not have its own terminal or console, therefore you will not be able to interact as a user with the batch file. A workaround for this, using two batch files, is described in example 4 at right.
Also, be aware that you cannot use EXEC to execute "internal" operating system commands. In Microsoft Windows, these are commands such as RENAME, COPY, DIR, etc. Internal commands are built into the Windows (or other Operating System) command processor and are not themselves separate executable programs. EXEC only works on executable programs or on batch files. To use internal commands such as these, you will need to put them in a batch file and use EXEC to invoke the batch file. See example 5 at right. Alternately, you can use the Windows CMD "external" command to invoke an internal command as example 6 shows.
If you need to set some environment values you can use the env keyword. (In Windows, the environment values are changed or set in batch files or the command window with the Windows' SET command.) The env keyword must be followed by a single string variable or literal string that contains one or more pair of environment parameter names and values of the form name=value. If there are more than one name/value pair, the pairs must be separated by a comma. Here is an example of the env portion of an EXEC command:
EXEC ... ENV "path=C:/someFolder/;%path%,temp=c:/temp"
The command string must be enclosed in double quotes as shown. The command string can be any (non-internal) command that would be valid for the operating system's command processor.
EXEC "c:/program files/someProgram/someProgram.exe arg1"
If your command string includes one or more arguments, be aware that blank spaces in an argument may cause it to be interpreted as multiple arguments, divided at the spaces. For example:
EXEC NOWAIT "C:/Program Files/Microsoft Office/Office12/excel.exe C:/Users/John/Documents/data folder/myData.csv"
fails because the "data folder" has a space in it. To make it work, separate the argument into its own quoted string like this:
EXEC NOWAIT "C:/Program Files/Microsoft Office/Office12/excel.exe" "C:/Users/John/Documents/data folder/myData.csv"
If the above doesn't work, or you need to set up some environment variables or other conditions requiring more than one operating system command, then the solution is to put the commands in a batch file. Then invoke the batch file with the EXEC command:
Forward slashes work best for file paths in the EXEC command even in a Windows system, but if you prefer (for Windows only), you can use backslashes. The inconvenience is that you have to use double backslashes because in Java strings the backslash is an escape character. So, you would have to say:
You can have any number of EXEC commands in a Resampling Stats program, but each one generates a new, separate operating system process in which to run its command string.
Example 1. The following example shows a typical use of the EXEC command. Assume you have some height and weight data for a group of people and you have a program called BMICalculator.exe that can compute the Body Mass Index for a person given the height and weight. Also assume that the BMICalculator.exe can read its input data from a file if the name of the input file is given on the command line by a "-i" switch. Assume also that the BMICalculator.exe takes the name of its output file from a "-o" command line switch.
DATA (5 5.38 5.09 5.12 5.55 5.92 5.4 4.57) Height DATA (115.8 156.3 132.6 196.1 159.7 179.5 164.9 141.4) Weight
WRITE file "file1.txt" height weight
EXEC "C:/program files/bmi/BMICalculator.exe -ifile1.txt -ofile2.txt"
READ file "file2.txt" BodyMassIndex PRINT table height weight bodyMassIndex
The above program writes its data to the file called "file1.txt", then uses EXEC command to invoke the BMICalculator.exe program, which reads file1.txt and stores its data in file2.txt. The Resampling Stats program waits at the EXEC command until the BMICalculator program completes. Then the Resampling Stats program reads file2.txt into the BodyMassIndex variable. Finally, it produces this (simulated) output:
Height Weight BodyMassIndex 5 115.8 22.5 5.38 156.3 26.3 5.09 132.6 25.1 5.12 196.1 36.5 5.55 159.7 25 5.92 179.5 25 5.4 164.9 27.5
4.57 141.4 32.9
Example 2. As another example, you might generate data in Statistics101 and want to load it into another program, such as Microsoft Excel. Here's a simple program to demonstrate that possibility:
NORMAL 10000 100 10 myData
HISTOGRAMDATA percent binsize 2 myData binNumberVec binStartsVec binCenterVec frequencyVec
WRITE file "C:/Users/John/Documents/myData.csv" HEADING binNumberVec binStartsVec binCenterVec frequencyVec
EXEC NOWAIT "C:/Program Files (x86)/Microsoft Office/Office12/excel.exe" "C:/Users/John/Documents/myData.csv"
Example 3. Another example of EXEC command usage:
EXEC "java" "-jar" "c:/program files (x86)/Statistics101/statistics101.jar" "-xC:/Program Files (x86)/Statistics101/ExamplePrograms/MonteCarloExamples/LeakyPondSimulation/5_ScatterChartHistories.txt" NOWAIT
Example 4. When EXEC starts a batch file, the operating system process running the batch file will not have a console or terminal window. If you need such a window, then you have to use two batch files. The first batch file, which has no console window, is used to invoke the second one, which will have a console. Here's an outline of this method in Windows:
Here's the EXEC command in your Resampling Stats program:
where batchFile1 contains the following:
START /wait "c:\fullPathName2\batchFile2.bat"
and where batchFile2 contains your DOS commands, for example:
CD "someWorkingDirectoryPathName" command1 command2 pause
Example 5. Here's an example of how you can use Statistics101 to create its own batch file and then execute it. This program creates an output file, pauses so you can verify that the file exists and look at its contents. Then when you click the continue button, it creates a batch file in the same directory, then pauses so you can verify the contents of the batch file. Then when you click continue again, it executes the batch file. The batch file deletes both the originally created file and itself. (You might get a warning message when it deletes itself.)
'Create output file
FOREACH n 1,25
OUTPUT file "c:/users/john/documents/outputTest.txt" \
append "line %1.5f\n" n
PRINT "Output file created."
'Create batch file to delete the output file and itself
'(i.e., the batch file will delete itself also)
OUTPUT file "c:/users/john/documents/outputTest.bat" \
OUTPUT file "c:/users/john/documents/outputTest.bat" append \
PRINT "Batch file created."
'Execute the batch file
EXEC verbose "c:/users/john/documents/outputTest.bat"
PRINT "Batch file completed"
Example 6. This example shows how to invoke an "internal" OS command without using a batch file. Say you wanted to get a disk directory listing of the "C:\" directory into a file. If you had a command window open you would just type in:
dir c:\ > "C:\Users\John\Documents\dirList.txt
To do the same thing using the Resampling Stats EXEC command, you would do this:
EXEC verbose "cmd" "/c" "dir" "c:\\" ">" "C:\\Users\\John\\Documents\\dirList.txt"
Note that the "/c" switch tells the cmd process to terminate after producing the directory. Also, the double backslashes are required.