100% found this document useful (1 vote)
106 views

Java System Calls

The Runtime class allows running external commands from Java. It provides methods like exec() to run processes and getProcess() to return a Process object representing the subprocess. The Process object has methods like getErrorStream(), getInputStream(), exitValue() and waitFor() to handle input/output and status of the subprocess. exec() can take a command string or string array as arguments. The streams can be read using BufferedReader to get output and errors. exitValue() returns the exit code and waitFor() waits for process completion.

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
106 views

Java System Calls

The Runtime class allows running external commands from Java. It provides methods like exec() to run processes and getProcess() to return a Process object representing the subprocess. The Process object has methods like getErrorStream(), getInputStream(), exitValue() and waitFor() to handle input/output and status of the subprocess. exec() can take a command string or string array as arguments. The streams can be read using BufferedReader to get output and errors. exitValue() returns the exit code and waitFor() waits for process completion.

Uploaded by

ErwinMacaraig
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Java System Calls

1. The class java.lang.Runtime class features a static method called getRuntime( ) which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external commands by invoking the Runtime classs exec( ) method.
Runtime rt = Runtime.getRuntime(); Process p = rt.exec( external command);

2. Overloaded exec( ) methods


public public public public Process Process Process Process exec(String command) exec(String[ ] cmdArray) exec(String command, String[ ] envp) exec(String[] cmdArray, String[ ] envp)

3. The exec( ) method creates an operating system specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system. 4. exitValue( ) method of the Process class returns the exit value for the subprocess. It will thrown an IllegalThreadStateException if the external process has not yet completed. You can use the waitFor( ) method that causes the current thread to wait until the process represented by this Process object has terminated.
int exitVal = p.exitValue( ); int exitVal = p.waitFor();

5. To handle output text from an external program, we make use of IO streams, assuming that the external command is an executable program.
InputStream stderr = p.getErrorStream(); InputStreamReader isr = new InputStreamReader(stderr); BufferedReader br = new BufferedReader(isr); InputStream stdin = p.getInputStream(); InputStreamReader reader = new InputStreamReader(stdin); BufferedReader b = new BufferedReader(reader);

6. For an non-executable program (especially in Windows), execute either command.com or cmd.exe depending on the Windows operating system you use.
String cmd[] = new String[3]; cmd[0] = command.com; cmd[1] = /C; cmd[2] = dir *.java; p = rt.exec(cmd);

MyExecutioner.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import java.io.*; import java.util.*; public class MyExecutioner { public static void main(String[] args){ try { Runtime rt = Runtime.getRuntime(); String[] cmd = new String[3]; cmd[0] = "cmd"; cmd[2] = args[0]; cmd[1] = "/C"; Process p = rt.exec(cmd); //Error detection here BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream())); String line = ""; while( (line = br.readLine()) != null ){ System.out.println("ERROR: " + line); } //get output from the subprocess BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); while((line = in.readLine()) != null){ System.out.println("OUTPUT: " + line); } int exitVal = p.waitFor(); System.out.println("Is there an error? " + exitVal); } catch(Throwable t){ t.printStackTrace(); } }

10/30/2012

} /* getErrorStream() - returns the input stream connected to the error output of the subprocess getInputStream() - returns the input stream connected to the normal of the subprocess getOutputStream() - returns the output stream connected to the normal input of the subprocess */

You might also like