Section 4.5. The Runtime Class


4.5. The Runtime Class

Let's discuss one last way to get to the underlying (Linux) system information. Be warned, though, that this is the least portable approach of all we have mentioned.

4.5.1. exec()

Familiar to C/C++ programmers, the exec() call in the Java Runtime class does much the same thing. It gives you a way to start another program outside of the current Java Virtual Machine. In doing so, you can connect to its standard in/out/err and either drive it by writing to its standard in, or read its results from its standard out. (Yes, that's correctwe write to its input and read from its output. If that sounds wrong, think it through. Our Java code is on the opposite side of the I/O fence. The external program's output becomes our input.)

Example 4.4 shows a Java program that can invoke an arbitrary Linux program. The output of the program is displayed.

Example 4.4. Java program to execute any Linux program
 import java.io.*; public class Exec {   public static void   main(String [] args)     throws IOException   {     String ln;     Process p = Runtime.getRuntime().exec(args);     BufferedReader br = new BufferedReader(                           new InputStreamReader(                             p.getInputStream()));     while((ln = br.readLine()) != null) {       System.out.println(ln);     }     System.out.println("returns:" + p.exitValue());   } // main } // class Exec 

The command-line arguments are taken to be the command to be executed and its arguments. For example:

 $ java Exec ls -l 

Be aware that in this example, only the standard output is captured and displayed from the invoked process. Error messages written to standard err will be lost, unless you modify the program to handle this. We leave that as an exercise for the reader.

Check your Linux knowledgesee if you understand the distinction. If you invoke the sample Exec program as:

 $ java Exec ls -l *.java 

the shell does the wildcard expansion before invoking the Java runtime. The *.java becomes many files listed on the command line (provided that you have .java files in this directory). If you try to pass the *.java tHRough literally to exec(ls '*.java') it will likely return an error (which won't be displayed using our example code) and you'll see a nonzero return status (e.g., 1). That's because ls doesn't expand the *. The shell does that. So ls is looking for a single file named *.java, which we hope doesn't exist in your directory.

4.5.2. Portability

Be aware that the more environment-specific code you build, the less portable your application becomes. It's not uncommon to use a properties file as a way to parameterize your program, to customize its behavior in a given installation. But keep these to a minimum to stay portable. Avoid invoking other programs, they are likely not available in all environments where Java can run. Java's claim to "compile once, run anywhere" is amazingly trueprovided you keep away from logic in your program that goes looking for trouble.



    Java Application Development with Linux
    Java Application Development on Linux
    ISBN: 013143697X
    EAN: 2147483647
    Year: 2004
    Pages: 292

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net