我正在使用 Java SE 6 并希望通过预定义程序在外部进程中打开文件。例如,目前我正在为 pdf 文件使用以下代码:
Windows :
public static Process openFile(File file) {
return Runtime.getRuntime().exec("C:/Program Files (x86)/Adobe/Reader 10.0/Reader/AcroRd32.exe \""+file.getAbsolutePath()+"\"");
}
Ubuntu Linux:
public static Process openFile(File file) {
return Runtime.getRuntime().exec("/usr/bin/evince \""+file.getAbsolutePath()+"\"");
}
在 Windows 上,这适用于所有文件。但是在 ubuntu 上,只要文件路径中有空格,它就会尝试打开多个文件。这是一个例子:
contract.pdf -> works on windows and ubuntu
contract 1 (copy).pdf -> works only on windows, ubuntu tries to open 3 different files (contract, 1, and (copy).pdf)
我需要什么特殊字符来告诉 ubuntu 它应该将带有空格的文件路径作为一个文件来处理?
提前感谢您的帮助! 最好的问候
最佳答案
我会尝试使用 Desktop.open
Launches the associated application to open the file.
If the specified file is a directory, the file manager of the current platform is launched to open it.
或者你可以试试
return Runtime.getRuntime().exec(new String[]{
"/usr/bin/evince", file.getAbsolutePath() });
关于Ubuntu Linux 上的 Java SE 6 : How to open files externally by Runtime. getRuntime().exec()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13891690/