java 调用cmd命令

阅读更多




package com.xiangsoft.mainborad;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Mainboard extends JFrame{

	private JPanel contentPane;
	
	JTextArea jta = new JTextArea("界面数据:");
	public static void main(String[] args) {
			Mainboard mb = new Mainboard();
			mb.setVisible(true);
	}
	
	public Mainboard(){
		this.setLocation(400, 300);
        setTitle("设置窗体大小");// 设置窗体标题
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默认关闭方式
        setSize(800,600);// 设置窗体大小
        contentPane=new JPanel();// 创建内容面板
        contentPane.setLayout(new BorderLayout(0,0));
        setContentPane(contentPane);// 设置内容面板
    //    JLabel label=new JLabel("界面数据"); // 创建标签控件    
        JScrollPane sp = new JScrollPane(jta);   
        contentPane.add(sp,BorderLayout.CENTER);// 添加标签控件到窗体
        
        JButton jb = new JButton("代理测试");
        jb.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				InputStream ins = null;
				String[] cmd = new String[]{ "cmd.exe", "/C", "ipconfig" };
				
				try{
					
					Process process = Runtime.getRuntime().exec(cmd);

					ins = process.getInputStream();

					BufferedReader reader = new BufferedReader(new InputStreamReader(ins));

					String line = null;

					while ((line = reader.readLine()) != null){
						jta.append(line+"\n");
						EventQueue.invokeLater(new Runnable(){

							public void run() {
								try {
									Thread.sleep(3000L);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							
						});
						System.out.println(line);
					}

					int exitValue = process.waitFor();
					System.out.println("返回值:" + exitValue);

					process.getOutputStream().close();
					
				}catch (Exception ex){
						ex.printStackTrace();
				}
				
			}

			});
        
        contentPane.add(jb,BorderLayout.EAST);
        
    }
}




你可能感兴趣的:(java,swing,调用console,runtime,process)