Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a non-java, cross platform way to launch the associated application for a certain file type?

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.

like image 262
FeatureCreep Avatar asked Sep 26 '09 10:09

FeatureCreep


People also ask

Why is Java considered cross platform?

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.

Is Java truly cross 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.


2 Answers

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.

like image 79
Jörg W Mittag Avatar answered Sep 22 '22 17:09

Jörg W Mittag


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.

like image 20
IgKh Avatar answered Sep 18 '22 17:09

IgKh