So I am writing a program in Java that needs to be able to run on Windows or Linux and needs to be able to make requests to the command line such as dir or ls. It then needs to get the resultant output from these commands and AFTERWARDS prompt for another command. 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;
BufferedReader brErrorStream = null;
String errorStream;
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()));
while((stdOut = brStdOut.readLine()) != null)
{
System.out.println(stdOut);
}
while((errorStream = brErrorStream.readLine()) != null)
{
System.err.println(errorStream);
}
process.waitFor();
}
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);
}
}
I should mention that I'm currently testing on a Windows 7 machine and that anything after a # on a line is considered as a comment.
When I run this, say using dir, the result comes back just fine, but try running it with ls (which gives a message saying that it's not a recognized command in Windows) and the error stream may be printed before the next prompt (??) (which is desirable), after the next prompt, or partially before and partially after the prompt. Is there a way to consistently get the error message printing BEFORE the prompt?
Here is an example of what's happening now:
??dir
Volume in drive C is Windows7_OS
Volume Serial Number is SomeNumbers
Directory of C:\Users\BlankedOut\Documents\Java\Eclipse\Workspace\Prog1
02/05/2012 03:48 PM <DIR> .
02/05/2012 03:48 PM <DIR> ..
02/05/2012 03:48 PM 301 .classpath
02/05/2012 03:48 PM 387 .project
02/05/2012 03:48 PM <DIR> .settings
02/05/2012 08:39 PM <DIR> bin
02/05/2012 08:39 PM <DIR> src
2 File(s) 688 bytes
5 Dir(s) 861,635,362,816 bytes free
??ls
??'ls' is not recognized as an internal or external command,
operable program or batch file.
ls
'ls' is not recognized as an internal or external command,
operable program or batch file.
??exit
My other question regards when I try running a command such as date. In the DOS window, this gives a 2-line response and waits for more input, but in Java, I only get the first line of the response (although with dir I seem to get all the lines), but then after that first date line, the program just hangs... does anyone know why this might be happening?
Thanks a lot!