请问,
用Process  p  =  Runtime.getRuntime().exec("*.exe");  
这种方法开启的exe进程,如何让Java程序知道这个exe进程是否还存在?我试过Process.exitValue()这个方法
但是没有用!
在进程管理器里能够看到*.exe确实还在运行,
但exitValue()的返回值永远都是0 !不知道是怎么会事!有没有其他更好的方法啊?
能告诉我吗?
非常感谢!!!

解决方案 »

  1.   

    process.exitValue() 方法没有问题的,请尝试下面的代码package hermit.test;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /**
     * UI程序开发模板
     * @author Lex Chen
     * @date 20060414
     */
    public class UITester extends JFrame implements ActionListener{
    static final long serialVersionUID = 16192289000L ; 
    private final int WIDTH = 600; //预设宽度
    private final int HEIGHT = 480; //预设高度
    JPanel contentPane;  //主容器
    boolean lock = false;
    JPanel panel;
    public UITester(){
    enableEvents(AWTEvent.WINDOW_EVENT_MASK); //激活事件处理
    }
    //程序入口
    public static void main(String[] args) {
    UITester frame = new UITester();
        frame.createUI();
        frame.validate();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
          frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
          frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }
    public void createUI(){
    Font font = new Font("Courier New",Font.PLAIN,12);
    this.setSize(WIDTH,HEIGHT);
    this.setResizable(false);
    this.setTitle("Swing Template");
    this.setFont(font);
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(null);
        JButton button = new JButton("Start");
        button.setActionCommand("start");
        button.setMargin(new Insets(1,1,1,1));
        button.setFont(font);
        button.setBounds(100,85,100,30);
        button.addActionListener(this);
        panel = new JPanel();
        panel.setBounds(100,100,300,200);
        panel.setBorder(BorderFactory.createEtchedBorder());
        panel.setLayout(null);
        contentPane.add(panel);
        panel.add(button);
        button = new JButton("Query");
        button.setActionCommand("query");
        button.setMargin(new Insets(1,1,1,1));
        button.setFont(font);
        button.setBounds(100,130,100,30);
        button.addActionListener(this);
        panel.add(button);
    }
    protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    // 处理关闭窗口事件
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    System.exit(0);
    }
    }
    Process process = null;
    public void actionPerformed(ActionEvent e){
    String cmd = e.getActionCommand();
    if("start".equals(cmd)){
    try{
    process = Runtime.getRuntime().exec("notepad.exe");
    }catch(Exception ee){
    ee.printStackTrace();
    }
    }else if("query".equals(cmd)){
    try{
    int value = process.exitValue();
    JOptionPane.showMessageDialog(this, "ExitValue="+value);
    }catch(Exception ee){
    JOptionPane.showMessageDialog(this, "Still Running");
    }
    }
    }
    }
      

  2.   

    谢谢,
    楼上调的是自己写的东西嘛!
    而我调的是别人写的exe可执行文件啊!
    不一样的!
      

  3.   

    如果是自己通过Process  p  =  Runtime.getRuntime().exec("*.exe");  创建的进程,那么要判断他是否结束可以通过 p.waitfor();一直等待,直到其结束。p.exitValue()这个方法只是得到返回值而已.
    如果不是自己的程序创建的进程,比如任务管理器什么的。
    Java下似乎没什么好方法,调用"tasklist",再通过匹配应该是可以实现的.
      

  4.   

    楼主如果有疑问可以发邮件到
    [email protected]
    或者访问http://icecooly.cn  留言
      

  5.   

    好的,非常感谢。
    我调用的不知自己写的数据库备份的exp.exe。
    我去试试。=)
      

  6.   

    我给你的代码不是调自己写的啊,调用的是notepad.exe来做的例子,理论上调用其它
    程序不应该有问题的,exp.exe好像是oracle的程序吧