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++?
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 thatxdg-open
does the right thing for you.If that's not sufficient,
xdg-open
uses platform tools to do its work, falling back toxdg-mime
. This reads.desktop
files. You can probably do the same...