62,623
社区成员
发帖
与我相关
我的任务
分享
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);
}
}