Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to launch the default (native) application for a given file from Java?

I am displaying a list of files; i.e. xls, doc, pdf, odt etc., in my Java application (Eclipse RCP). When the user clicks on the file, I want to launch the appropriate (according to what the OS thinks) native application, just like it happens in Windows Explorer or the Finder.

And while I am here: It would be nice to also display the same icons that Finder or Explorer use for the different file types.

Is there a library or Eclipse plugin for this?

like image 893
Thilo Avatar asked Feb 18 '09 06:02

Thilo


People also ask

How to open existing file in Java?

Java FileInputStream class is used to open and read a file. We can open and read a file by using the constructor of the FileInputStream class. The signature of the constructor is: public FileInputStream(File file) throws FileNotFoundException.

How do you load a file in Java?

Example 1: Java Program to Load a Text File as InputStream txt. Here, we used the FileInputStream class to load the input. txt file as input stream. We then used the read() method to read all the data from the file.

How do I change the content of a file in Java?

1) open the file for input 2) read the file 3) close the file 4) change the data in the file 5) open the file for output 6) write the changed data to the file 7) close the file Any book on Java will have the basics of Input and Output.


1 Answers

What you want is java.awt.Desktop:

Desktop.getDesktop().open( file );
like image 189
Joseph Gordon Avatar answered Oct 10 '22 15:10

Joseph Gordon