在 Java 上使用外部应用程序打开文件
2022-09-02 23:14:43
当您不知道与哪个应用程序关联的应用程序时,如何从 Java 应用程序打开文件。另外,因为我使用的是Java,所以我更喜欢独立于平台的解决方案。
当您不知道与哪个应用程序关联的应用程序时,如何从 Java 应用程序打开文件。另外,因为我使用的是Java,所以我更喜欢独立于平台的解决方案。
在 JDK1.6 中,java.awt.Desktop
类可能很有用。
public static void open(File document) throws IOException {
Desktop dt = Desktop.getDesktop();
dt.open(document);
}
File file
Desktop.getDesktop().open( file );
从 Java 1.6 开始
在此之前,您可以检查此问题
总结
它看起来像这样:
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.
}