执行完Runtime.getRunt.exec()这个方法之后如何怎样得到输出???比如执行完:netsata /ano?????

解决方案 »

  1.   

    Process p = Runtime.getRuntime().exec(text);
    p.getInputStream();
    p.getErrorStream();
    参考import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;public class CmdFrame extends JFrame { private static final long serialVersionUID = -2650474785662737262L;

    private JLabel jl ;
    private JTextField jtf ;
    private JTextArea jta ;
    private JButton jb ;
    private JPanel jp ;
    private JScrollPane jsp;

        public CmdFrame() {
         jp = new JPanel();
         jl = new JLabel("输入命令:");
         jtf = new JTextField(50);
         jta = new JTextArea();
         jb = new JButton("button");
         jsp = new JScrollPane();
         lanuchFrame();
        }
        
        private void lanuchFrame(){
         jp.setLayout(new FlowLayout());
         jp.add(jl);
         jp.add(jtf);
         add(jp,BorderLayout.NORTH);
        
         jta.setRows(10);
         jsp.setViewportView(jta);    
         add(jsp,BorderLayout.CENTER);
         add(jb,BorderLayout.SOUTH);
         MyListener myListener =  new MyListener();
         jb.addActionListener(myListener);
         this.setBounds(400,300,800,300);
         pack();
         this.setVisible(true);
        }
        
        private class MyListener implements ActionListener{
        
         public void actionPerformed(ActionEvent e) {
         String text = jtf.getText();
         if(text == null || "".equalsIgnoreCase(text.trim())){
         JOptionPane.showMessageDialog(null,"请输入命令");
         return ;
         }else {
         jta.setText("");
         try{
         Process p = Runtime.getRuntime().exec("cmd /c "+text);
         DealStream errStream = new DealStream(p.getErrorStream(),"Err");
         DealStream outStream = new DealStream(p.getInputStream(),"Out");
         new Thread(errStream).start();
         new Thread(outStream).start();
         int exitValue = p.waitFor();
         jta.append("exitValue -- > " + exitValue);
         }catch(Exception ex){
         ex.printStackTrace();
         }   
         }
         }    }
        
        private class DealStream implements Runnable{
         private InputStream is ;
         private String type ;
        
         public DealStream(InputStream is,String type){
         this.is = is;
         this.type = type ;
         }
        
         public void run(){
         try{
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String temp = null;
    while((temp = br.readLine()) != null){
    jta.append(type+"-->" + temp+"\n");
    }
         }catch(Exception e){
         e.printStackTrace();
         }finally{
         try{
         is.close();
         }catch(Exception ex){
         ex.printStackTrace();
         }
         }
         }   
        }   
        public static void main(String[] args){
         new CmdFrame();
        }   
    }