1. 程式人生 > >java開啟指定檔案(執行指定程式exe)

java開啟指定檔案(執行指定程式exe)

1


	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!");
		}
	}



2

3