Ubuntu Linux 上的 Java SE 6 : How to open files externally by Runtime. getRuntime().exec()?

标签 java windows ubuntu runtime exec

我正在使用 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/

相关文章:

c++ - 循环文件映射会降低性能

c++ - 如何在 Vista/Server 2008 上使用 std::shared_mutex?

python - 为什么有关 python 命令行启动的记录没有保存在历史记录中

ubuntu - 调试时如何加载 gdbinit?

java - 在apache http客户端中,如何将StringBody中的Content-Type保持为空或null?

java - Android通过程序设置bottomNavigationBar中图标和文本的颜色失败

windows - 无法从另一个内部调用 .bat 文件 - "not recognized as an internal or external command"错误

ubuntu - 如何启用 openLDAP 以在 Ubuntu 上使用 LDAP over SSL

java - Spring-remoting SimpleHttpInvokerRequestExecutor 在不应该重试请求时重试请求

java - 是我还是这个课件词条有错别字?