I am trying to make a Java GUI for the windows command prompt with some simple commands. I am having some issues with additional input. I can run the "dir" command but when I run the "del" command, I need to give a confirmation, but I cant seem to get it to print out the confirmation message.
public static void main(String args[])
{
try {
Process p = Runtime.getRuntime().exec("cmd /c dir");
read(p);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void read(Process p)
{
try{
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while((line=input.readLine()) != null) {
System.out.println(line);
}
}
catch(IOException e1) { e1.printStackTrace(); }
}
Output:
Volume in drive E has no label.
Volume Serial Number is E9ED-7E32
Directory of E:\Code\workspace\Project
04/11/2012 11:07 PM <DIR> .
04/11/2012 11:07 PM <DIR> ..
04/11/2012 09:53 PM 301 .classpath
04/11/2012 09:53 PM 383 .project
04/11/2012 09:53 PM <DIR> .settings
04/12/2012 12:09 AM <DIR> bin
04/12/2012 12:09 AM <DIR> src
04/11/2012 11:07 PM <DIR> test
2 File(s) 684 bytes
6 Dir(s) 429,106,937,856 bytes free
But running this will cause it to hang at line=input.readLine()
public static void main(String args[])
{
try {
Process p = Runtime.getRuntime().exec("cmd /c del test");
read(p);
OutputStream oStream = p.getOutpuStream();
BufferedWriter sWriter = new BufferedWriter(newOutputStreamWriter(oStream));
sWriter.write("y");
sWriter.newLine();
oStream.close();
read(p);
} catch (IOException e) {
e.printStackTrace();
}
}
How do I prevent this hanging? I am also concerned that the confirmation "y" isn't being sent correctly. I feel like it should be read the output and write the input, but this is the way that I found at multiple sites online. Any help would be appreciated.