Nawaf Abdu Nawaf Abdu - 1 year ago 94
Java Question

Java how can I execute/run specific file by his path

I want to execute/run this file using Java.
The path is "/usr/local/"

I tried:

public class Main {

public static void main(String[] args) {

String[] cmd = {"cd /usr/local","./"};
try {
Process p = Runtime.getRuntime().exec(cmd);

} catch (IOException e) {
// TODO Auto-generated catch block

} }

but it doesn't work. Is there another way?

Answer Source

You can use ProcessBuilder (see and set the working directory using directory(...) instead of cd command.

In the end something like this:

ProcessBuilder pb = new ProcessBuilder("/usr/bin/bash","/usr/local/");
Process p = pb.start();
IOUtils.copy(p.getInputStream(), System.out);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download