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

}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值