Use java.lang.Runtime.exec()

Use the java.lang.Runtime.exec method to call programs or commands from within your Java™ program. Using java.lang.Runtime.exec() method creates one or more additional thread-enabled jobs. The additional jobs process the command string that you pass on the method.

Note: The java.lang.Runtime.exec method runs programs in a separate job, which is different than the C system() function. The C system function runs programs in the same job.

The actual processing that occurs depends on the following items:

Processing different types of commands

The following table indicates how java.lang.Runtime.exec() processes different kinds of commands and shows the effects of the os400.runtime.exec system property.

Type of command Value of os400.runtime.exec system property
EXEC (default value) QSHELL
java command Starts a second job that runs the JVM. The JVM starts a third job that runs the Java application. Starts a second job that runs Qshell, the shell interpreter. Qshell starts a third job to run the Java application, program, or command.
program Starts a second job that runs an executable program (i5/OS™ program or i5/OS PASE program).
CL command Starts a second job that runs an i5/OS program. The i5/OS program runs the CL command in the second job.
Note: When calling a CL command or CL program, make sure that the job CCSID contains the characters that you pass as parameters to the called command.

The processing in the second or third job runs concurrently with any Java virtual machine (JVM) in the original job. Any exit or shutdown processing in those jobs does not affect the original JVM.

os400.runtime.exec system property

You can set the value of the os400.runtime.exec system property to EXEC (the default value) or QSHELL. The value of os400.runtime.exec determines whether java.lang.Runtime.exec() uses the EXEC interface or Qshell.

Using a value of EXEC instead of QSHELL has the following advantages:

You should use java.lang.Runtime.exec() to run Qshell only when backward compatibility requires it. Using java.lang.Runtime.exec() to run Qshell requires that you set os400.runtime.exec to QSHELL.

The following illustration shows how using a value of QSHELL launches a third job, which consumes additional system resources. Remember that using a value of QSHELL decreases the portability of your Java program.

Figure 1. Using a value of QSHELL for the os400.runtime.exec system property
This graphic shows that using a value of QSHELL for the os400.runtime.exec
system property causes a process to launch a second and then a third job.

Also, when you use a value of QSHELL, passing a CL command to java.lang.Runtime.exec() requires specific syntax. For more information, see the following example for calling a CL command.

For information about setting os400.runtime.exec, see List of Java system properties.

Related concepts
IBM i5/OS PASE native methods for Java
Teraspace storage model native methods for Java
Comparison of Integrated Language Environment and Java
Interprocess communications
Related tasks
Use the Java Native Interface for native methods