Recently I had an idea to use Java to implement interactive commands like the CMD console in windows. Today I finally wrote a simple example. we will release it to share with you.
The Code is as follows:
Package jconsole;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. util. collections;
/**
* <Title> "jconsole"-Java simulated console </title>
* <P>
* This class uses Java to implement the Windows cmd console. By obtaining the current running process,
* Execute relevant command information. Implements an interactive cmd console. <Br>
* <Br>
* Recently I have an idea to use Java to perform interactive commands like the CMD console in windows,
* Today I finally wrote a simple example.
*
* </P>
* @ Author Wei renyan
* @ Version 0.9
*/
Public class jconsole {
// Private process proconsole;
/**
* Obtain the process for executing the current command
*
* @ Param strcommand
* Run the command
* @ Return pro precess
*/
Private process getprocess (string strcommand ){
Process pro = NULL;
Try {
// Run the current command and obtain the running process
Pro = runtime.getruntime(cmd.exe C (strcommand );
} Catch (ioexception e ){
// When cause ioexception, print the exception message;
System. Out. println ("Run this command:" + strcommand
+ "Cause exception, and the message is" + E. getmessage ());
}
// Return the process of running the command
Return pro;
}
/**
* Get the standard input stream of the current process
*
* @ Param pro
* Current Process
* @ Return in inputstream
*/
Public inputstream getinputsreamreader (process Pro ){
Inputstream in = NULL;
// Obtain the input stream of the current process
In = pro. getinputstream ();
// Return the input stream
Return in;
}
/**
* Get the error input stream of the current process
*
* @ Param pro
* Current Process
* @ Return Error inputstream
*/
Public inputstream geterrorsreamreader (process Pro ){
Inputstream error = NULL;
// Get the error stream of the current process
Error = pro. geterrorstream ();
// Error stream returned
Return Error;
}
/**
* Print the content in the input stream to the standard output.
*
* @ Param in
* Inputstream input stream
*/
Public void printmessage (final inputstream in ){
Runnable = new runnable (){
/*
* (Non-javadoc)
*
* @ See java. Lang. runnable # Run ()
*/
Public void run (){
Try {
/*
* Int ch; do {CH = in. Read ();
*
* If (CH <0) return; system. Out. Print (char) CH );
* System. Out. Flush ();} while (true );
*/
Bufferedreader buffer = new bufferedreader (
New inputstreamreader (in ));
String lines;
While (true ){
Lines = buffer. Readline ();
If (lines = NULL)
Return;
System. Out. println ("" + lines );
System. Out. Flush ();
}
} Catch (exception e ){
E. printstacktrace ();
}
}
};
Thread thread;
Thread = new thread (runnable );
Thread. Start ();
}
/**
* Initialize the CMD console.
*/
Public void run (){
Process pro;
Inputstream input;
Pro = getprocess ("cmd ");
Input = getinputsreamreader (Pro );
Printmessage (input );
}
/**
* @ Param ARGs
* @ Throws ioexception
*/
Public static void main (string [] ARGs) throws ioexception {
// Run the sub-process
Process pro = NULL;
Running in = New Processing (system. In );
// Input stream
Inputstream input;
// Error stream
Inputstream err;
Jconsole = new jconsole ();
// Initialize the CMD Console
Jconsole. Run ();
// Obtain the command to be executed from the input and print the feedback result
While (in. hasnextline ()){
String strcmd = in. nextline ();
If (strcmd. Equals ("exit ")){
System. Exit (0 );
}
// Execute the input command
Pro = jconsole. getprocess ("CMD/E: On/C" + strcmd );
Input = jconsole. getinputsreamreader (Pro );
Err = jconsole. geterrorsreamreader (Pro );
// Print the command running result
Int strtmp = input. Read ();
If (strtmp =-1 ){
Jconsole. printmessage (ERR );
} Else {
Jconsole. printmessage (input );
}
}
}
}