So I am writing a program to run commandline processes from Java. This needs to work on Windows and Linux; although, I am currently testing on Windows 7. I should also mention that anything after a # is considered a comment. With that, here is my code:
import java.io.*;
import java.util.*;
public class myShell
{
public static void main(String[] args)
{
Runtime runtime = Runtime.getRuntime();
Process process;
Scanner keyboard = new Scanner(System.in);
String userInput = "";
String osName = System.getProperty("os.name" );
BufferedReader brStdOut = null;
String stdOut;
String stdOutNextLine;
BufferedReader brErrorStream = null;
String errorStream;
String errorStreamNextLine;
while(userInput.compareToIgnoreCase("exit") != 0)
{
System.out.println();
System.out.print("??");
userInput = keyboard.nextLine();
int indexOfPound = userInput.indexOf('#');
if(indexOfPound == 0)
{
userInput = "";
}
else if(indexOfPound > 0)
{
userInput = userInput.substring(0, indexOfPound);
}
userInput = userInput.trim();
try
{
if(osName.contains("Windows"))
{
process = runtime.exec("cmd /c " + userInput);
}
else
{
process = runtime.exec(userInput);
}
brStdOut = new BufferedReader(new InputStreamReader(
process.getInputStream()));
brErrorStream = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
stdOut = "";
errorStream = "";
boolean firstStdOut = true;
boolean firstErrorStream = true;
long time = System.currentTimeMillis();
while(((stdOutNextLine = brStdOut.readLine()) != null) &&
((System.currentTimeMillis() - time) < 5000))
{
if(firstStdOut)
{
stdOut = stdOutNextLine;
firstStdOut = false;
}
else
{
stdOut = stdOut + "\n" + stdOutNextLine;
}
}
time = System.currentTimeMillis();
while(((errorStreamNextLine = brErrorStream.readLine()) != null)
&& ((System.currentTimeMillis() - time) < 5000))
{
if(firstErrorStream)
{
errorStream = errorStreamNextLine;
firstErrorStream = false;
}
else
{
errorStream = errorStream + "\n" + errorStreamNextLine;
}
}
System.out.println(stdOut + errorStream);
}
catch(Exception e)
{
System.out.println("Error executing: " + userInput);
System.out.println(e.getMessage());
}
try
{
brStdOut.close();
}
catch(Exception e)
{
}
try
{
brErrorStream.close();
}
catch(Exception e)
{
}
}
System.exit(0);
}
}
So, the problem I am having is when I run commands like date which in the commandline, would request further user input. For the time being, I'd like to just timeout these types of commands and get whatever would have been printed up to the time where I timeout the process.
So, if in the commandline date would return 2 lines and then wait for user input, I just want my program to print those 2 lines and then move on. The problem is, that as written, the timeout in the while loops for getting the output from the commandline doesn't seem to do anything, the program runs exactly the same with or without the (System.currentTimeMillis() - time) < 5000 qualifier. I also tried writing the while loops as:
while((stdOutNextLine = brStdOut.readLine()) != null)
{
if(firstStdOut)
{
stdOut = stdOutNextLine;
firstStdOut = false;
}
else
{
stdOut = stdOut + "\n" + stdOutNextLine;
}
if((System.currentTimeMillis() - time) > 5000)
{
throw new TimeoutException();
}
}
and then catching the TimeoutException to work with the String from there, but that also didn't seem to actually timeout the process.
Any other ideas?
Thank you!