Take the 2-minute tour ×

I want to make a java program witch automatically opens a specified program to a given file for example a mp4 file should be opened with a Converter to convert it to avi

So my question is: is it possible to open a file in java with a given program, but not the default one? And if yes, how?

share|improve this question
Do you mean, as other answerers here apparently understand your question, that you want to write a Java program that runs some other program to open a particular file? Or do you mean that you want to make your Java program the default "open" program for a certain file type, so anytime someone double-clicks a file of that type in the directory, your program runs? –  Jay May 16 '12 at 20:24

3 Answers 3

As long as you know the exact path or the program has a known envirment variable, it's quite easy:

    Process p = Runtime.getRuntime().exec("notepad Q:/Test/tmp.txt");
    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
catch(Exception k)
    JOptionPane.showMessageDialog(null, k.getMessage());

This opens the file tmp in notepad.

share|improve this answer
The BufferedReader there is just a remnant from an old code bit I had, you don't actually need that, Just the Process line. –  Martin May 16 '12 at 20:22
Thanks it worked, you really helped me out :) –  fisch12345 Jul 2 '12 at 18:17

You will need to use Runtime.exec() method, but you must read When Runtime.exec() won't beforehand. The program that you want to run must accept a exec parameter that represent the file to be opened.

I know that this is not an answer for you question, but if you want to open a file using the default application (registered by the OS) you should use the "open" method of java.awt.Desktop class.

share|improve this answer
Note that using a ProcessBuilder to create the Process helps to implement at least one of the recommendations of the linked article (merge the streams and it is easier to consume them). +1 for being the only one to mention the article that the OP will need to read if going down this path. –  Andrew Thompson May 16 '12 at 20:41

You can run any program you want with any file you want with Process.exec()

(Actually getting it to open the default program is trickier IMHO)

share|improve this answer

Your Answer


