程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java實現返回cmd控制台所產生的信息

java實現返回cmd控制台所產生的信息

編輯:關於JAVA

以前寫JAVA時想練練手寫一個編輯器,涉及到JAVA代碼的編譯部分,考慮調用用javac.exe來編譯,一下這段代碼是用於將編譯後產生的錯誤信息反饋到GUI上.同理,在cmd下執行的任何命令都可以反饋到GUI中.

//運行效果圖

//以下代碼用到了SWT相關的包

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("執行命令");
 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("宋體", 10, SWT.NONE));
 text_1.setBounds(10, 61, 385, 277);
 final Label label = new Label(shell, SWT.NONE);
 label.setText("請在下列輸入框中輸入CMD命令");
 label.setBounds(10, 9, 294, 12);
 //
}
 
//執行命令
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();
     }
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved