Runtime Class

This is a class in Java you can use to grab the Runtime Environment of the machine where the Java Program is running. You can use this among other things to execute operating system commands or scripts.

The process is:

  1. Get the Runtime instance by calling getRuntime() method you inherit.
  2. Using the Runtime instance (say called r), invoke the exec("xxx") method where "xxx" is the command you wish to execute. This will return a Process object.
  3. You can monitor the progress or retrieve the output of the executed command from the Proecess object of step 2

 

Example using a Servlet (yes, we have not gone over Servlets but, look at the steps inside of the code, you could easily put it in the main method of your application). This code simply gets a filelisting of a certain directory

CheckDirectory.java