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.
asked Oct 25, 2019 at 10:26
Alberto
3591 gold badge5 silver badges12 bronze badges
1 Answer 1
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"
);
answered Oct 25, 2019 at 12:51
Joni
112k14 gold badges151 silver badges201 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Alberto
This way it works correctly but I don't understand the reason. In the code I provide, I indicate the directory where you have to run the application. In fact, if instead of trying to run vBoxManager I run a "dir", it works correctly on the indicated directory.
Joni
When you try to run
VBoxManage the operating system looks for an executable file called VBoxManage.exe in a few folders. "c:/Program files/Oracle/VirtualBox" is not one of them and that's why you get the error. The list of folders is controlled by "PATH environment variable" - search for that phrase to learn more. "Directory" would set the program's current working directory, it does not help the operating system find the executable.Alberto
I'm sorry but I still don't understand the problem. Using the "directory" method, I indicate the working directory where the application I want to run is, and even then, it does not run it. Instead, it does run a "dir", for example
lang-java
VBoxManagevs.vboxmanage