Locking in the While Program

3

I'm working on a project and I'm having problems with it, this program I'm posting is similar to what I'm working on.

The problem is this: it arrives at while , the program does its job, then hangs up and does not exit.

I have tried everything. Could anyone give any suggestions?

public class CommandZ {

private static String Command;
private static Scanner scan;


public static void main(String[] args) {        
    scan = new Scanner(System.in);


        System.out.println("Shell: ");
        Command = scan.nextLine();

        ProcessBuilder pb = new ProcessBuilder("powershell.exe", "-Command", Command);
        Process p;
        try {
            p = pb.start();
        } catch (IOException e) {
            System.out.println("Failed to start powershell");
            return;
        }


        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        System.out.println("Begin!");
        try {
            //PROBLEMA AQUI
            while((line = bufferedReader.readLine()) != null){
                System.out.println(line);
                //Imprimi as linhas, e não da sequência no programa..




            }
        } catch (IOException e) {
            System.out.println("Failed to read line");
            return;
        }

        System.out.println("Exit");


    }
    
asked by anonymous 19.10.2014 / 19:54

4 answers

1

This code can solve your problem.

The source is this conversation , specifically at the end of the conversation.

Code:

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;


public class Gobbler implements Runnable {

    private PrintStream out;
    private String message;

    private BufferedReader reader;

    public Gobbler(InputStream inputStream, PrintStream out) {
        this.reader = new BufferedReader(new InputStreamReader(inputStream));
               this.out = out;
        this.message = ( null != message ) ? message : "";
    }

    public void run() {
        String line;

        try {
            while (null != (line = this.reader.readLine())) {
                out.println(message + line);
            }
            this.reader.close();
        } catch (IOException e) {
            System.err.println("ERROR: " + e.getMessage());
        }
    }
}


class PowerConsole {

    private ProcessBuilder pb;
    Process p;
    boolean closed = false;
    PrintWriter writer;

    PowerConsole(String[] commandList) {
        pb = new ProcessBuilder(commandList);
        try {
            p = pb.start();
        } catch (IOException ex) {
            throw new RuntimeException("Cannot execute PowerShell.exe", ex);
        }
        writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
        Gobbler outGobbler = new Gobbler(p.getInputStream(), System.out);
        Gobbler errGobbler = new Gobbler(p.getErrorStream(), System.out);
        Thread outThread = new Thread(outGobbler);
        Thread errThread = new Thread(errGobbler);
        outThread.start();
        errThread.start();
    }

    public void execute(String command) {
        if (!closed) {
            writer.println(command);
            writer.flush();
        } else {
            throw new IllegalStateException("Power console has ben closed.");
        }
    }

    public void close() {
        try {
            execute("exit");
            p.waitFor();
        } catch (InterruptedException ex) {
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        PowerConsole pc = new PowerConsole(new String[]{"powershell.exe", "-NoExit", "-Command", "-"});
        System.out.println("========== Executing dir");
        pc.execute("dir"); 
//        System.out.println("========== Executing cd\");
//        pc.execute("cd \"); Thread.sleep(2000);
//        System.out.println("========== Executing dir");
//        pc.execute("dir"); Thread.sleep(2000);
//        System.out.println("========== Executing cd \temp");
//        pc.execute("cd \temp"); Thread.sleep(2000);
//        System.out.println("========== Executing dir");
//        pc.execute("dir"); Thread.sleep(2000);
//        System.out.println("========== Executing cd \bubba");
//        pc.execute("cd \bubba"); Thread.sleep(2000);
        System.out.println("========== Exiting .... bye.");
        pc.close();
    }
}
    
20.10.2014 / 11:25
0

I found a solution on stackoverflow.com :

ProcessBuilder proc = new ProcessBuilder().inheritIO().command(
                "powershell.exe", "-Command", Command);
        proc.redirectErrorStream(true);
        Process p = proc.start();
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
    
20.10.2014 / 19:42
0

I think your problem is because "powershell.exe" is waiting for command at runtime. And because of this the while "hangs" in execution. Because Process is waiting for new data to be read.

I found this talking link about it.

I also ran some tests running on Windows. Calling cmd.exe and passing commands, it locks in while. Well I imagine he's waiting for new commands to continue returning in InputStreamReader. But when I run only one command like ipconfig. It ends the program normally.

Well I hope I have helped ^^

    
21.10.2014 / 19:05
-1

Your mistake has to be logical. You have not ensured that the variable line reaches the value null so the while repeats indefinitely. You should do this at the end of while , added before comment:

...
while((line = bufferedReader.readLine()) != null){
    System.out.println(line);
    line = null;
 //Imprimi as linhas, e não da sequência no programa..
...
    
20.10.2014 / 07:52