Process类 destroy() 方法为什么不起作用

yilinhust 2007-12-12 03:03:12
上面这个程序的process.destroy()方法不起作用,但下面的却可以
请问这是怎么回事?


package demo;

import java.util.Properties;
import java.io.IOException;
import java.lang.InterruptedException;

public class OpenBrowser{
static Process process = null;

public static void main(String[] args) throws Exception{

Properties properties = System.getProperties();
String osName = properties.getProperty("os.name");

System.out.println (osName);

if (osName.indexOf("Linux") != -1) {
process = Runtime.getRuntime().exec("htmlview");
} else if (osName.indexOf("Windows") != -1){
process = Runtime.getRuntime().exec("explorer http://www.163.com");
// p = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE http://www.sina.com");
} else {
throw new RuntimeException("Unknown OS.");
}

if(process != null){
new Thread(new Runnable(){
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {}

process.destroy();

System.out.println ("进程被销毁!");
}
}).start();

int exitcode=process.waitFor();
System.out.println("finish:"+exitcode);
}
}
}



package demo;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

public class OpenIE extends JFrame {
Process p = null;

public OpenIE() {
JButton button = new JButton("Open");
JButton buttonClose = new JButton("Close");

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
String[] strs = {"C:\\Program Files\\Internet Explorer\\iexplore.exe",
"http://dzh.mop.com/topic/readSub_5661107_0_0.html"};
p = Runtime.getRuntime().exec(strs);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

buttonClose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
p.destroy();
}
});

Container c = this.getContentPane();
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
this.getContentPane().add(button);
this.getContentPane().add(buttonClose);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 200, 200);
}


public static void main(String[] args) {
new OpenIE().setVisible(true);
}

}



...全文
806 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanlingyun 2007-12-13
  • 打赏
  • 举报
回复
问题应该出在process = Runtime.getRuntime().exec("explorer http://www.163.com");
把它改成process = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE http://www.sina.com");就可以成功销毁了,我估计是直接写explorer,启动的时候可以找到它的路径,但销毁时去销毁不了。

62,623

社区成员

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

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