public class OpenSpecialFile{
public static void main(final String[] args) throws IOException {
openWindowsExe();
openExe();
openFile();
}
// 用 Java 調用windows系統的exe文件,比如notepad,calc之類
public static void openWindowsExe() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
final String command = "notepad";// 記事本
process = runtime.exec(command);
} catch (final Exception e) {
System.out.println("Error win exec!");
}
}
// 調用其他的可執行文件,例如:自己製作的exe,或是 下載 安裝的軟件.
public static void openExe() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;
try {
process = runtime.exec("C:\\Program Files\\Notepad++\\notepad++.exe");
} catch (final Exception e) {
System.out.println("Error exec!");
}
}
// 打開其他任意格式的文件,比如txt,word等
public static void openFile() {
final Runtime runtime = Runtime.getRuntime();
Process process = null;//
final String cmd = "rundll32 url.dll FileProtocolHandler file://F:\\ECT項目資料\\建立EMF工程.txt";
try {
process = runtime.exec(cmd);
} catch (final Exception e) {
System.out.println("Error exec!");
}
}
}
//第一種方式(指定程序)
Runtime.getRuntime().exec("D:\\Program Files\\Notepad++\\notepad++.exe"+" "+filePath);
//第二種(默認程序)
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", filePath});
//第三種(默認程序)
Runtime.getRuntime().exec("rundll32 url.dll FileProtocolHandler"+" "+filePath);