回答
假設您希望啓動Windows程序notepad.exe
,您正在尋找exec
函數。你可能想調用是這樣的:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\path\\to\\notepad.exe C:\\path\\to\\file.txt");
例如,在我的機器記事本是位於C:\Windows\notepad.exe
:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("C:\\Windows\\notepad.exe C:\\test.txt");
這將打開記事本的文件test.txt打開編輯。
注意,您還可以指定exec
這是要執行的工作目錄的第三個參數 - 因此,您可以啓動相對於程序工作目錄存儲的文本文件。
使用SWT,你可以啓動任何 如果你想模擬雙擊windows中的文本,這是不可能的,只有一個普通的JRE。您可以使用本機庫一樣SWT和使用下面的代碼打開一個文件:
org.eclipse.swt.program.Program.launch("c:\path\to\file.txt")
如果你不希望使用第三方lib中,你應該知道,你知道在哪裏的notepad.exe是(或者它在路徑中可見):
runtime.exec("notepad.exe c:\path\to\file.txt");
阿帕奇common-exec是處理外部進程執行一個好的圖書館。
更新:更完整的回答你的問題,可以發現here
嘗試
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().edit(file);
} else {
// dunno, up to you to handle this
}
確保該文件存在。感謝Andreas_D指出了這一點。
您可能想要添加有關所需Java版本的詳細信息以便使其工作。 – 2010-08-15 12:39:16
嘿,不知道。涼。但是 - 第二行必須閱讀'Desktop.getDesktop()。edit(file);'。並且該文件必須被創建,否則它會抱怨'IllegalArgumentException'。 – 2010-08-15 12:42:51
謝謝@Andreas,修復它。確保文件存在是guilgamos的工作的一部分:)無論如何,+1還是不錯的 – whiskeysierra 2010-08-15 15:35:16
在IDE(Eclipse)中,它包含關於「C:\ path \ to \ notepad.exe C:\ path \ to \ file.txt」。 所以我用了以下這個工作讓我保持我和我的IDE快樂:o) 希望這會幫助其他人。
String fpath;
fPath =System.getProperty("java.io.tmpdir")+"filename1" +getDateTime()+".txt";
//SA - Below launches the generated file, via explorer then delete the file "fPath"
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("explorer " + fPath);
Thread.sleep(500); //lets give the OS some time to open the file before deleting
boolean success = (new File(fPath)).delete();
if (!success) {
System.out.println("failed to delete file :"+fPath);
// Deletion failed
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
(假設你想記事本打開「myfile.txt的」 :)
ProcessBuilder pb = new ProcessBuilder("Notepad.exe", "myfile.txt");
pb.start();
String fileName = "C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\Student Project\\src\\studentproject\\resources\\RealWorld.chm";
String[] commands = {"cmd", "/c", fileName};
try {
Runtime.getRuntime().exec(commands);
//Runtime.getRuntime().exec("C:\\Users\\Riyasam\\Documents\\NetBeansProjects\\SwingTest\\src\\Test\\RealWorld.chm");
} catch (Exception ex) {
ex.printStackTrace();
}
如果你開始用命令的命令行記事本你可以做到這一點,最好的:記事本開始
String[] startNotePadWithoutAdminPermissions = new String[] {"CMD.EXE", "/C", "start" "notepad" };
保存數組的字符串命令,並給它像參數在exec
Process runtimeProcess = Runtime.getRuntime().exec(startNotepadAdmin2);
runtimeProcess.waitFor();
- 1. 如何在記事本或記事本++中打開VBA文件
- 2. 如何在java中的記事本中打開.log文件?
- 3. 如何啓動記事本++在空中打開文本文件?
- 4. 如何在Windows PowerShell中打開記事本++ python文件?
- 5. 如何用C++在記事本++中打開文件?
- 6. 如何在.NET中的記事本中打開文本?
- 7. 檢查是否在記事本中打開文本文件
- 8. 在記事本中以文本文件格式打開資源
- 9. 如何在批處理文件中用記事本打開文件
- 10. 打開記事本
- 11. 使用java物理打開記事本
- 12. 編程記事本 - 打開使用Java
- 13. 如何用記事本++打開一個類文件?
- 14. 如何從批處理文件中打開兩個記事本文件?
- 15. 從程序在記事本++中打開Python文件
- 16. 在記事本中打開VS2010 cs文件
- 17. 使用Python獲取在記事本中打開哪個文件
- 18. C#雙擊文件在記事本標籤中打開
- 19. 在網絡瀏覽器中打開記事本文件
- 20. Powershell dot sourcing在記事本中打開文件
- 21. 在c中用記事本++閱讀的打開文件#
- 22. 使用VB.net在記事本++中打開多個TXT文件
- 23. 在記事本中打開的.pl文件
- 24. 使用VBA在記事本中打開.tbl文件
- 25. 如何在Java中打開文件?
- 26. 如何在java中打開.pdf文件
- 27. 在MFC中的記事本中打開文本
- 28. 如何導入在記事本中打開的.xls文件都是html? vb.net
- 29. 打印記事本文本文件
- 30. 如何在Java中打開文本文件
「記事本」是什麼意思?在Windows上使用一個糟糕的文本編輯程序,或TextArea控件?原諒我承擔「事情」,但聽起來你不知道Swing/AWT的基礎知識。 – aviraldg 2010-08-15 11:24:51
...你想打開記事本程序,或者你在記事本中創建的文本文件嗎? – Stephen 2010-08-15 11:25:07