void jButton1_actionPerformed(ActionEvent e) {
//must be use a new thread.
Thread t = new Thread(new Runnable(){
public void run(){
try {
//String[] arrCommand = {"javaw", "-jar", "d:/Unicom/Salary/Salary.jar"};
// Process p = Runtime.getRuntime().exec(arrCommand);
Process p = Runtime.getRuntime().exec("notepad");
p.waitFor();
System.out.println("return code: " + p.exitValue());
} catch (IOException e) {
System.err.println("IO error: " + e);
} catch (InterruptedException e1) {
System.err.println("Exception: " + e1.getMessage());
}
}
});
t.start();
}
运行后,点击jButton1调用了Windows中的记事本应用程序。这里,新线程使用了Runnable接口,这是一种常用的技巧。另外,还必须要捕获IOException和InterruptedException两个异常。对于调用带有参数的复杂程序,要使用字符串数组代替简单的字符串,我在上面的代码注释了。