以上各位缺少异常处理! import java.awt.*; import java.awt.event.*; import javax.swing.*;public class RunExe extends JFrame implements ActionList { JButton open = new JButton("打开"); Runtime r = Runtime.getRuntime(); Process p = null;
public RunExe() { JPanel pane = new JPanel(); open.addActioListener(this); pane.add(open); setContentPane(pane);
super("运行"); setSize(300,500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { RunExe run = new RunExe(); run.show(); }
public static void main(String[] margs) throws Exception{
String cmd = "C:\\Program Files\\Microsoft Office\\Office\\winword.exe "
+ ".\\xy.doc";
Runtime.getRuntime().exec(cmd);
}
}这样可以打开工作目录下的xy.doc。
import java.awt.event.*;
import javax.swing.*;public class RunExe extends JFrame implements ActionList
{
JButton open = new JButton("打开");
Runtime r = Runtime.getRuntime();
Process p = null;
public RunExe()
{
JPanel pane = new JPanel();
open.addActioListener(this);
pane.add(open);
setContentPane(pane);
super("运行");
setSize(300,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
RunExe run = new RunExe();
run.show();
}
public void actionPerformed(ActionEvent e)
{
try {
p=r.exec("C:\\Program Files\\Microsoft Office\\Office\\winword.exe ");
p.waitFor();
} catch (Exception e) {};
}
}
直接用windows里的start命令,他的效果等同于双击该文件/目录
如
Runtime.getRuntime().exec("cmd/c start abc\def\hij.doc");start http://www.csdn.net
start mailto://[email protected]
......