Nawaf Abdu Nawaf Abdu - 11 months ago 40
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?


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);