从我用
java编写的应用程序我想打开一个文件夹,使用操作系统文件资源管理器.
我使用Desktop.open(新文件(路径))
这在windows上工作正常,但在ubuntu 11.10(linux)上它不起作用.
使用Desktop.open打开文件确实可以在ubuntu和windows上运行.
使用介于两者之间的步骤:
文件fPath =新文件(fPath)
并使用fPath.exists()和fPath.isDirectory()测试它们都给出了true.
使用Desktop.open(新文件(路径))给我这个例外:
java.io.IOException: Failed to show URI:file:/and/here/the/path/I/use/
at sun.awt.X11.XDesktopPeer.launch(Unknown Source)
at sun.awt.X11.XDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
我还没能在苹果电脑上测试这个,但我希望Desktop.open(新文件(路径))是系统独立的……
顺便说一句,完整的代码:
Desktop desktop = null;
// Before more Desktop API is used, first check
// whether the API is supported by this particular
// virtual machine (VM) on this particular host.
if (!Desktop.isDesktopSupported()) {
// show Error
return;
}
desktop = Desktop.getDesktop();
String path = "here the path ";
// by the way: I use System.getProperty("file.separator") as file seperator
try {
File fPath=new File(path);
if(!fPath.exists()){
// show Error
return;
}
if(!fPath.isDirectory()){
// show Error
return;
}
desktop.open(new File(path));
} catch (IOException e) {
log.severe(e.getMessage());
e.printStackTrace();
// show Error
return;
}
一些额外的信息:
操作系统:Linux(3.0.0-16-通用 – amd64)
Java:1.6.0_30-b12
Java home:/opt/java/64/jre1.6.0_30