Before writing Java, want to practice handwriting an editor, involving the compilation of Java code, consider the call with Javac.exe to compile, this code is used to compile the error feedback to the GUI. Similarly, any commands executed under CMD can be fed back into the GUI.
Run Effect chart
The following code uses an SWT-related package
import java.io.IOException;
import Java.io.InputStream;
import Org.eclipse.swt.SWT;
import Org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import Org.eclipse.swt.widgets.Button;
import Org.eclipse.swt.widgets.Display;
import Org.eclipse.swt.widgets.Label;
import Org.eclipse.swt.widgets.Shell;
import Org.eclipse.swt.widgets.Text;
import Com.swtdesigner.SWTResourceManager;
public class Cmdshell {
private Text text_1;
private text text;
protected shell shell;
/**
* Launch the application
* @param args
*/
public static void Main (string[] args) {
try {
Cmdshell window = new Cmdshell ();
window.open ();
} catch (Exception e) {
E.printstacktrace ();
}
}
/**
* Open the window
*/
public void Open () {
Final Display display = Display.getdefault ();
createcontents ();
Shell.open ();
shell.layout ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ())
Display.sleep ();
}
}
/**
* Create contents of the window
*/
protected void Createcontents () {
Shell = new Shell (SWT. MIN | Swt. Close | Swt. TITLE | Swt. RESIZE);
shell.setimage (Swtresourcemanager.getimage (Cmdshell.class, "/org/eclipse/jface/dialogs/images/title_ Banner.png "));
shell.setsize (415, 375);
shell.settext ("Cmdshelldemo");
Final Button button = New button (Shell, SWT. NONE);
Button.addselectionlistener (New Selectionadapter () {
public void widgetselected (Selectionevent e) {
Cmdshell ();
}
});
button.setimage (Swtresourcemanager.getimage (Cmdshell.class, "/org/eclipse/jface/dialogs/images/message_ Info.gif "));
button.settext ("execution order");
Button.setbounds (310, 25, 85, 24);
Text = new text (Shell, SWT. BORDER);
Text.setbackground (Swtresourcemanager.getcolor (202, 228, 240));
Text.setbounds (10, 27, 294, 22);
text_1 = new Text (Shell, SWT. V_scroll | Swt. BORDER | Swt. WRAP | Swt. H_scroll);
Text_1.setforeground (Swtresourcemanager.getcolor (255, 0, 0));
Text_1.setfont (Swtresourcemanager.getfont ("XXFarEastFont-Arial"), SWT. NONE));
Text_1.setbounds (10, 61, 385, 277);
Final Label label = new label (Shell, SWT. NONE);
label.settext ("Please enter CMD command in the following input box");
Label.setbounds (10, 9, 294, 12);
//
}
//execute command
private void Cmdshell () {
int read;
try {
Process PS = Runtime.getruntime (). EXEC (Text.gettext ());
InputStream is = Ps.getinputstream ();
InputStream Ts=ps.geterrorstream ();
StringBuffer out = new StringBuffer ();
byte[] b = new byte[1024];
Read=ts.read (b);
while (read = Ts.read (b))!=-1) {
Out.append (New String (b, 0, read));
}
Text_1.append (New String (out));
}
catch (IOException ex) {
Ex.printstacktrace ();
}
}
}