1

I would like to open a file with its associated application and a couple of command line parameters.

E.g. the_app.exe -someoption file.app, the_app.exe being the associated application for opening the .app files.

I was looking to the Desktop class with the hope to find either

  • a Desktop.getDesktop().open(file,options), or
  • a Desktop.getDesktop().getOpenFileHandler(file) and then build upon that with Process and ProcessBuilder

Neither do exist. Any other suggestion ?

lvr123
  • 524
  • 6
  • 24
  • If you don't know what is the application that will be used to open the file, how do you know what options to use? – fonkap Sep 20 '20 at 08:32

1 Answers1

1

In java you can use Runtime to run programs.

Runtime.getRuntime().exec("program");

You can also use "cmd" to run command-line commands.

Then, if option parameters weren't needed, you could simply use 'start' command.

//this will open notepad in my pc
Process pr = Runtime.getRuntime().exec("cmd /c start test.txt");

If you really need to pass commands you can use some auxiliar commands and build your own command-line:

With 'assoc' you retrieve the filetype

With 'ftype' you retrieve the application for a filetype

Then:

private static String command(Runtime rt, String command) throws IOException {
    Process pr = rt.exec("cmd /c " + command);
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(pr.getInputStream()))){
        return reader.readLine();
    }
}

public static void main(String[] args) throws IOException {
    //without parameters is an easy job
    Process pr = Runtime.getRuntime().exec("cmd /c start test.txt");

    //with parameters things get messy
    Runtime rt = Runtime.getRuntime();
    String out1 = command(rt, "assoc .txt");
    System.out.println(out1);
    String assoc = out1.split("=")[1];
    String out2 = command(rt, "ftype " + assoc);
    System.out.println(out2);
    String app = out2.split("=")[1].split(" ")[0]; //error prone, consider using regex
    command(rt, app + " /P test.txt"); //This will print (/P) the file using notepad

}

Of course, none of these are portable at all.

For more info:

Running Command Line in Java

Best way to get file type association in Windows 10 from command line?

https://www.addictivetips.com/windows-tips/file-app-association-command-prompt-windows-10/

How to open file with default application in cmd?

fonkap
  • 2,469
  • 1
  • 14
  • 30
  • 1
    Exactly the same answer I started typing last week and discarded afterwards, upon realizing that 1. the syntax for the command assigned in ftype is far from standard and getting the actual command to run from it is, as you said, error prone; 2. if OP knows what parameters to pass to the associated application... that means they already know which application it is, so there's no need for any of this. (But +1 anyways.) – walen Sep 21 '20 at 10:09
  • Thanks. Anyway, the "assoc" command returns a null value, while (in Win7) the Explorer recognizes the file type (in my case a "mscz" file, ("Compressed MuseSocre File"). Assoc should not return null. To investigate. To your remark about already knowing the application to use, yes I do know it. But I don't where it is installed and under which name (the name might differ from versions, the location is unique (OS, ...)). – lvr123 Sep 21 '20 at 14:38
  • That is weird.. did you remember the '.' (dot) before the extension? (I mean "assoc .mscz") You can also run "assoc" without any parameter to see all the associations. – fonkap Sep 21 '20 at 15:35
  • I did. The extension is not in a full "assoc" command, neither is its file type in a full "ftype" command. – lvr123 Sep 21 '20 at 15:50
  • Then I don't know. I suppose you could "properly" associate the extension and filetype using assoc and ftype, but I don't think this is practical in your case... I'm sorry I wish I could help more. – fonkap Sep 21 '20 at 16:09
  • 1
    Maybe something wrong with the way the application is registred in the registry... Thanks anyway – lvr123 Sep 21 '20 at 16:26
  • Just checked with other more common file type. E.g. "assoc .jpg" returns also "unknow type". – lvr123 Sep 30 '20 at 19:33
  • That's weird, in my pc: assoc .jpg -> .jpg=jpegfile – fonkap Oct 01 '20 at 15:53