0

I'm trying to run a powershell command (get status from printer) in intellij but for some reasons it doesn't run from the java. The program does not give an error and just delayed. Maybe i have not some access, but I don't know which one.

In CMD this command work correctly.

public static void main(String[] args) {
        String printerName = "HP LaserJet 1022n";
        ProcessBuilder builder = new ProcessBuilder("powershell.exe", "get-wmiobject -class win32_printer | Select-Object Name,Status | where {$_.Name -eq '" + printerName + "'}");
        String fullStatus = null;
        Process reg;
        builder.redirectErrorStream(true);
        try {
            reg = builder.start();
            fullStatus = IOUtils.toString(reg.getInputStream(), "UTF-8"); //in this moment program is delaying
            reg.destroy();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        System.out.print(fullStatus);
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

0

The IOUtils.toString waits until the InputStream terminates, but it's not gonna happen because the InputStream waits until the program terminates and this happens in the next line. The probably best way to get what you want is to read the InputStream line by line. (Maybe use a BufferedReader see)

Lukas Resch
  • 148
  • 15