Open any kind of application with BASH

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[]){
      try{
          String filename = args[0];
          System.out.println(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 ??

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x