0

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
4
  • 2
    Fails how exactly? Commented Oct 25, 2019 at 10:26
  • You use a different case for the name of the program with the ProcessBuilder. VBoxManage vs. vboxmanage Commented Oct 25, 2019 at 10:31
  • I tryed both but doesn't work Commented Oct 25, 2019 at 10:33
  • java indicates that can't find the application Commented Oct 25, 2019 at 11:17

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.