怒怒~! 如何让java知道某个exe进程是否存在?非常谢谢!!!

xxTonyxx 2007-06-22 11:06:09
请问,
用Process p = Runtime.getRuntime().exec("*.exe");
这种方法开启的exe进程,如何让Java程序知道这个exe进程是否还存在?

我试过Process.exitValue()这个方法
但是没有用!
在进程管理器里能够看到*.exe确实还在运行,
但exitValue()的返回值永远都是0 !

不知道是怎么会事!

有没有其他更好的方法啊?
能告诉我吗?
非常感谢!!!
...全文
334 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iori97king 2007-06-23
  • 打赏
  • 举报
回复
楼主如果有疑问可以发邮件到
icecooly@163.com
或者访问http://icecooly.cn 留言
iori97king 2007-06-23
  • 打赏
  • 举报
回复
如果是自己通过Process p = Runtime.getRuntime().exec("*.exe"); 创建的进程,那么要判断他是否结束可以通过 p.waitfor();一直等待,直到其结束。p.exitValue()这个方法只是得到返回值而已.


如果不是自己的程序创建的进程,比如任务管理器什么的。
Java下似乎没什么好方法,调用"tasklist",再通过匹配应该是可以实现的.
xxTonyxx 2007-06-23
  • 打赏
  • 举报
回复
谢谢,
楼上调的是自己写的东西嘛!
而我调的是别人写的exe可执行文件啊!
不一样的!
jinggangshi 2007-06-23
  • 打赏
  • 举报
回复
关注
LexChen 2007-06-23
  • 打赏
  • 举报
回复
我给你的代码不是调自己写的啊,调用的是notepad.exe来做的例子,理论上调用其它
程序不应该有问题的,exp.exe好像是oracle的程序吧
xxTonyxx 2007-06-23
  • 打赏
  • 举报
回复
好的,非常感谢。
我调用的不知自己写的数据库备份的exp.exe。
我去试试。=)
LexChen 2007-06-22
  • 打赏
  • 举报
回复
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");
}
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧