如果您不知道文件與哪個應用程序相關聯,那麼如何從java應用程序打開文件。另外,因爲我使用的是Java,所以我更喜歡獨立於平臺的解決方案。使用Java上的外部應用程序打開文件
17
A
回答
40
使用JDK1.6,java.awt.Desktop
類可能會有用。
public static void open(File document) throws IOException {
Desktop dt = Desktop.getDesktop();
dt.open(document);
}
2
你可以用Windows上的bat文件和Unix上的bat文件破解一些東西,但那不會很有趣。
我認爲你最好的選擇是JDesktop Integration Components (JDIC)。特別是,Desktop類具有您正在尋找的方法。
編輯:顯然,我落後於時代,因爲這已經集成到Java 1.6中。無論如何,如果你正在使用早期的Java,它可能仍然有用。
+0
提供的鏈接已損壞。你需要解決這兩個問題。謝謝。 – 2008-12-24 04:39:49
5
File file
Desktop.getDesktop().open(file);
由於Java 1.6
此之前,你可以check this question
摘要
這將是這個樣子:
Runtime.getRuntime().exec(getCommand(file));
public String getCommand(String file){
// Depending on the platform could be
//String.format("gnome-open %s", fileName)
//String.format("open %s", fileName)
//String.format("cmd /c start %s", fileName)
// etc.
}
相關問題
- 1. 在iPad上的外部應用程序中打開PDF文件
- 2. 使用外部程序打開外部文件
- 3. 在我的應用程序中打開外部應用程序
- 4. 從JavaFX打開外部應用程序
- 5. 從通用Windows應用程序打開外部應用程序
- 6. Android工作室從應用程序打開外部文件
- 7. 打開使用特定應用程序的MP3文件java
- 8. 使用C程序在外部打開文件
- 9. 使用delphi應用程序傳遞參數打開外部應用程序
- 10. 使用sql server從sql命令打開外部文件或應用程序
- 11. Android,在我的應用程序內部打開一個外部應用程序
- 12. 使用iOs上的默認應用程序打開文件
- 13. 使用Java應用程序打開文件
- 14. 從小部件打開應用程序
- 15. 用應用程序打開文件
- 16. 部署java應用程序以使用JRE打開
- 17. 從外部應用程序中的瀏覽器打開文本文件
- 18. 如何從外部應用程序打開Outlook郵件
- 19. 「用...打開」Windows上的文件,用python應用程序
- 20. 如何將Java Web應用程序上的源包和lib jar中的.java文件使用的外部文件?
- 21. Apple Configurator:能夠打開外部應用程序的「鎖定到應用程序」
- 22. 從反應原生應用程序打開外部應用程序
- 23. BATCH防止用戶從程序外部打開文件?
- 24. 在Java應用程序上使用Facebook API外部Facebook
- 25. Android開發者::在應用程序內打開外部鏈接
- 26. 在Ubuntu上使用Java應用程序打開端口80
- 27. 打開外部程序
- 28. 打開外部程序
- 29. 從我的iOS應用程序打開PDF文件與特定的外部應用程序
- 30. 使用Monodroid的Android應用程序無法獲得外部程序來正確打開文件
這段代碼是否也適用於linux? – 2017-07-23 14:58:42
是的......但最好先調用`Desktop.isDesktopSupported()`或`Desktop.isSupported(action)`,參見Javadoc。 – RealHowTo 2017-07-27 19:55:52