5
I’m working on a project and I’m having problems with the same, this program I’m posting is similar to what I’m working on.
The problem is this: it arrives in while
, the program does its function, then hangs and does not come out.
I’ve tried everything. Could someone give some suggestion?
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");
}
Have you tried to isolate the problem? probably this
InputStreamReader
is that it’s hanging. I think you can’t read it the way you’re trying. Or maybe you need to finish this process to finish the stream. I’ve never done anything like this. Was this your idea? Have you read somewhere that you can do this? Any additional information can help.– Maniero
I’m trying to make like a framework to interact with the windows prompt, but I have this problem, and I can’t follow the program.. tried a bufferedReader.close after while.. didn’t work..
– user17270
I have doubts whether what you are trying to do is possible, at least in this way. It is obvious that one is hanging in the
while
any attempt to do something after it, will not be carried out. And I doubt that try to close the stream within thewhile
is also the solution. Even if it seems to work it smells really bad.– Maniero
close the stream inside while, it will read only the first line, and the rest is gone.. will be that there is no other way then to do this ?
– user17270
I tried, give a close after the while, he didn’t even get there...
– user17270
It’s bone.. Nothing helps... a gringo said I needed another thread to read the output and then add it at the end of the main method()
– user17270
You may have another solution, but you need to have in-depth knowledge of all Apis that interact with processes and streams, maybe have a notification system when there is something new in the stream and respond to this signaling instead of staying in loop. I think your problem is lower, I hope someone can give some idea to do different but I don’t think the problem is just the
while
. And thread alone will not solve anything, has to see how to use in a correct way (if it will help).– Maniero
I don’t use windows, I try Virtualmachine, but I did a test on another computer with windows 7 and the problem was the same...
– user17270
He reads everything, all the way out of the prompt.. but at the end, he stops the program
– user17270
tried the suggestions, the problem remains.. P
– user17270
Well, let’s wait to see if someone with more experience in Java appears with a path (Sunday things slow down...).
– Bacco
Thanks for your help!
– user17270