jmasterx jmasterx - 3 months ago 19
Java Question

Starting a process in Java?

Is there a way to start a process in Java? in .Net this is done with for example:

System.Diagnostics.Process.Start("processname");


Is there an equivalent in Java so I can then let the user find the application and then it would work for any OS?

Answer

http://www.rgagnon.com/javadetails/java-0014.html

import java.io.*;
public class CmdExec {

  public static void main(String args[]) {
    try {
      String line;
      Process p = Runtime.getRuntime().exec
        (System.getenv("windir") +"\\system32\\"+"tree.com /A");
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      while ((line = input.readLine()) != null) {
        System.out.println(line);
      }
      input.close();
    }
    catch (Exception err) {
      err.printStackTrace();
    }
  }
}

You can get the local path using System properties or a similar approach.

http://download.oracle.com/javase/tutorial/essential/environment/sysprop.html