使用外部程序打开外部文件 [英] Open external file with an external program
问题描述
我是Java开发人员,并且有一个新问题.我想用另一个程序打开文件.我可以使用带有以下代码的 java.awt.Desktop
类轻松做到这一点:
I'm a Java developer and I have a new question. I want to open a file with another program. I can do it easily with the java.awt.Desktop
class with this code:
public static void open(File document) throws IOException {
Desktop dt = Desktop.getDesktop();
dt.open(document);
}
但是有没有办法选择它用来打开文件的程序?我的程序仅适用于Ubuntu.我想要一个包含所有可以打开该文件的常规程序的列表(右键单击该文件后看到的所有选项).如果它是一个可执行文件,则应执行.Java有可能吗?如果不是,那么C ++是否可能?
But is there a way to choose which program it uses to open the file? My program is only for Ubuntu. I want a list with all regular programs that can open that file (all options you see with a right click on the file). And if it is an executable file it should just execute. Is this possible with Java? And if it isn't, Is it possible with C++?
推荐答案
这是可能的,但可能不如使用Desktop API那样容易,因为它可能只是在后台调用了 xdg-open
.
It's possible, but probably not as easy as using the Desktop API, as that probably just calls xdg-open
under the hood.
由于桌面API几乎可以肯定会在后台调用 xdg-open
(就像大多数精心设计的程序一样),因此您可以通过设置桌面来实现所需的效果 xdg-open
为您做正确的事.
As the Desktop API almost certainly calls xdg-open
under the hood (as most well-designed programs would do), you may be able to achieve the effect you desire by setting your desktop up such that xdg-open
does the right thing for you.
如果这还不够,那么 xdg-open
使用平台工具来完成其工作,然后退回 xdg-mime
.这将读取 .desktop
文件.您可能可以做同样的事情...
If that's not sufficient, xdg-open
uses platform tools to do its work, falling back to xdg-mime
. This reads .desktop
files. You can probably do the same...
这篇关于使用外部程序打开外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!