I have made a Java program that could open any application. Suppose there is a file name "*.jpg", the it would allow the OS to recognize the type of application and then open the default application. Another example: Suppose the file's name is "*.flv", then it would open up the default media player just like when you click an icon in nautilus but the only difference was that it was in command line. Here is the java program for that:

import java.awt.*;
import java.io.*;
public class OpenFile{
    public static void main(String args[]){
          String filename = args[0];
          Desktop.getDesktop().open(new File(filename));
      catch(Exception e){
          System.out.println("Sorry an exception occured. Could not perform the operation.");

Now this script leads to performance issues. Now I want to write a BASH script to do this task ??

  • so what is your question?
    – MattDMo
    Commented Apr 4, 2013 at 15:29
  • @MattDMo sorry! forgot to put that! Now I have edited the post. Commented Apr 4, 2013 at 15:31

1 Answer 1


There is already an external command for this. There is nothing new that needs to be written. The command is xdg-open. It will open a file based on its MIME type association. Here is an example:

xdg-open file.png
  • 1
    oh! thanks! It works fine for me! Pretty good actually. Commented Apr 4, 2013 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .