1

I need my java code to open a file based upon the default application. Thanks to How to open user system preferred editor for given file? which suggests a quality method to do it by

Runtime.getRuntime().exec("RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL "+file);

But the issue is that, once I select the application to open it up, it doesn't open the file. I do not know the reason for it.

Thanks

EDIT:

Desktop.getDesktop().open(file);

This opens in default application. I want the user to choose the application to open it up

Community
  • 1
  • 1
Jatin
  • 31,116
  • 15
  • 98
  • 163

1 Answers1

5

Use :

Desktop.getDesktop().open(file);

http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#open(java.io.File)

EDIT:

Here is the code to make your command work:

import java.io.File;
import java.io.IOException;

public class TestExec {

    public static void main(String[] args) throws IOException, InterruptedException {
        File file = new File("d:/Clipboard1.png");
        ProcessBuilder builder = new ProcessBuilder("RUNDLL32.EXE", "SHELL32.DLL,OpenAs_RunDLL", file.getAbsolutePath());
        builder.redirectErrorStream();
        builder.redirectOutput();
        Process process = builder.start();
        process.waitFor();
    }

}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117