Lecture 12A
Browsers
System Properties

Cursor Icons
MouseButtons
Keystrokes
Audio
Timing
Toolkit
AppletContext
Run Ext. Programs
Misc. Tools

Lectures

1A: Introduction
1B: Java Intro
2A: BuildingBlocks
2B: Objects
3A: More Objects
3B: Exceptions
       & Threads
4A: Waves
4B: Nuclear &
       Particle
5A: AWT
5B: More AWT
      & Graphics
6A:Detectors &
      Simulation
6B: LHC/Atlas &
     RandomSims    
7A: Swing
7B: Java2D
8A: Java Apps
8B: Dialogs &
    MoreClasses
9A: Java I/O
9B: Utilities,
        Unicode
10A: More
    Threading
10B: File
    Handling
11A: Array,Print,
    Images
11B: SimplePhysics
    Simulation
12A: Tips &
    Techniques
12B: More Tips &
    Techniques
13A: Satellite
    
Simulations
13B: Intro to Java
    Networking
14A: Java Servers
14B: HTTP Server
15A: ServerClient
15B: ServerClient
   Expt.Simulation
16A: Course
          Review
16B: ExerciseTest
        Discussion

    Contacts
    Description
    Exercises
    Index
    Outline
    Q&A
    Resources
    StudentInfoForm
    Student Pages
    What's New

 

Home : Lectures : Lecture 12A : Running External Programs
Running External Programs

Applications occasionally need to start other programs.

For example, one might want to start a browser.

The java.lang.Runtime and java.lang.Process classes are available for executing and communicating with external programs.

Here is code to run a MS-DOS batch file that does a directory listing. The program reads the output from this program and prints it out.

doDir.bat:

dir *.java

execDir.java

// Modified from Just Java
import java.io.*;
public class execDir {
 
  public static void main(String args[]) {
    try {
      Runtime rt = Runtime.getRuntime();       // step 1
 
      Process prcs = rt.exec("doDir.bat");     // step 2

      InputStreamReader isr =                  // step 3
        new InputStreamReader( prcs.getInputStream() );

      BufferedReader br = new BufferedReader(  // step 4.
                                        isr );
      String line;
      while  ((line = br.readLine()) != null)
                   System.out.println(line);

    } catch(IOException ioe) { System.out.println(ioe); }
  }
}

Note that this obviously involves the details of the particular host platform and OS.

For example, the exec method does not use a shell. If a shell is needed, then it can be run directly.

Communication can only be via the standard in, out, and err streams.

 

 

Home Lectures Resources Index Contacts Students


Physics Simulations with JavaTM
KTH, Kurskod: 5A1418
Curator: Clark S. Lindsey