0

Is there a way to list all virtual disks attached using Java? I've tried using ProccessBuilder to open diskpart and running command and then using InputStreamReader, save lines into an array to later extract vhd name. But after running diskpart command my program freezes.

try {
    commands.add("cmd.exe");
    commands.add("start");
    commands.add("diskpart");
    commands.add("list vdisk");

    ProcessBuilder builder = new ProcessBuilder(commands);
    Process p = builder.start();

    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}
luk2302
  • 55,258
  • 23
  • 97
  • 137
Maczosss
  • 23
  • 5
  • `diskpart` does not take `list vdisk` as an argument, so why did you think that would work? Did you try running `diskpart list vdisk` in a command prompt? Next time, make sure the command you're trying to run actually works, before trying to do it from Java. – Andreas Dec 04 '20 at 13:14
  • Although the output is different, perhaps using `powershell.exe -Command "Get-VirtualDisk"` would be better, since it does support running with an argument. – Andreas Dec 04 '20 at 13:17

0 Answers0