Open external file with an external program

2019-08-27 10:51发布

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);
}

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++?

标签: java file
1条回答
在下西门庆
2楼-- · 2019-08-27 11:21

It's possible, but probably not as easy as using the Desktop API, as that probably just calls xdg-open under the hood.

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.

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...

查看更多
登录 后发表回答