上面这个程序的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);
}}
请问这是怎么回事?
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);
}}
把它改成process = Runtime.getRuntime().exec("C:/Program Files/Internet Explorer/IEXPLORE.EXE http://www.sina.com");就可以成功销毁了,我估计是直接写explorer,启动的时候可以找到它的路径,但销毁时去销毁不了。