Java implementation returns the information generated by the CMD console

Source: Internet
Author: User
Tags final getcolor stringbuffer

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 ();


     }


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.