First, I found a couple of java specific questions and answers for this. I am looking for more "native", but cross platform solution, using C, C++, some kind of shell scripts, or, in my case, Qt.
So the question is, are there standard, cross platform, ways to programmatically open the associated application for certain file types. Or at least to find out if there are associated applications and be able to locate and launch them?
By cross platform I mean Windows, OSX and linux (gnome/kde). The use case is having a database with stored files as blobs that will be read on the three different targets.
Java is cross platform because a program's source code is compiled into an intermediate "bytecode" language. The bytecode is then executed by a Java Virtual Machine (Java interpreter) that was written for that particular hardware platform.
Java is cross-platform because the program's source code is compiled into bytecode. The bytecode is then executed by the Java JVM that has been written for the specific hardware platform.
I don't know of any cross-platform way.
In Windows, there is the start
command, which will launch the associated default application. (E.g. start foo.doc
will launch the default Word document editor, start http://StackOverflow.Com/
the default web browser and start mailto:[email protected]
the default mail app.)
In OS X there is the open
command, which does the same thing.
Linux is just an Operating System kernel. OS kernels don't know anything about "filetypes" or "MIME types" or "associated applications" or anything like that. Therefore, such a thing simply cannot exist for Linux.
The Freedesktop Group has a specification for an xdg-open
command, which works on all Freedesktop-compliant graphical desktops (be they Linux, FreeBSD, NetBSD, OpenBSD, DragonflyBSD, OpenSolaris or otherwise). However, it is obviously not guaranteed to work on non-Freedesktop systems and it is certainly not guaranteed to work on non-graphical systems.
In all three cases, this is a command line application, not a C or C++ API, but you can obviously call it via system
.
Since you have noted that you are using Qt, it's worth mentioning the QDesktopServices class, and especially the openUrl(QUrl) method. It does pretty much what you've described on all platforms supported by Qt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With