Alberto
Alberto

Reputation: 359

Trying to run an application from Java

I need to run the "VBoxManage vms list command" to see the virtual machines installed on a computer from a Java application.

The following code works correctly but only if I use the Runtime class but I would like to know why it fails if I use ProcessBuilder.

The code is the following:

public static void main(String[] args) throws IOException {
    String folder= "c:/Program files/Oracle/VirtualBox";
    List<String> comand = Arrays.asList(
        "VBoxManage",
        "list",
        "vms" 
    );
    ProcessBuilder pb = new ProcessBuilder()
            .directory(new File(folder))
            .command(comand);

    Process p = pb.start();

    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while((line=br.readLine()) != null){
        System.out.println(line);
    }
}

This works fine if I use the Runtime class with this code: Runtime runtime = Runtime.getRuntime(); Process p = runtime.exec("c:/Program files/Oracle/VirtualBox/vboxmanage list vms");

Thank you.

Upvotes: 0

Views: 54

Answers (1)

Joni
Joni

Reputation: 111219

Try using the full path of the executable, like you do when using Runtime.exec

List<String> comand = Arrays.asList(
    "c:/Program files/Oracle/VirtualBox/VBoxManage",
    "list",
    "vms" 
);

Upvotes: 1

Related Questions