How do I execute other applications?


Sometimes a pure java implementation doesn't quite cut it. If you need to run other applications from within your app (you won't be able to do this from an applet), then here's what you need to do.

Step One

Obtain a java.lang.Runtime instance. The java.lang.Runtime class allows you to execute other applications, but you'll need an object reference before you can begin.

   // Get runtime instance
   Runtime r = Runtime.getRuntime();

Step Two

Next, you'll make a call to execute your application. The exec call returns a Process object, which gives you some control over the new process. If you just need to start something running, then you might want to discard the returned process.

   // Exec my program
   Process p = r.exec ("c:\\myprog\\inc");

Step Three

Now, if you want to do something with your process (perhaps kill it after a certain time period, or wait until its finished), you can use the methods of the java.lang.Process class. Check the Java API documentation for more information - there's plenty of things you can do. Suppose you wanted to wait until its terminated, and then continue with your app. Here's how you'd do it.

   // Wait till its finished

Easy huh?