1

I need to download a file from a java server using socket, and then open it with the default windows program. I've explored for several solution, but none fits perfectly for what I need. The aim is to temporarily open a file with an external default program and then delete the file when the external program exits. I've found these solution:

  1. Desktop.getDesktop.open(MyFile), but It seems it can't handle the returned value to check the external program exit
  2. ProcessBuilder, it handles the external program exit, but It does not open the "default Windows program", and so I need to check every file extension to run the associated command. This solution is quite "close", but I can't know in advance all types of file to open
  3. Apache Commons Exec, that seems to be the same as 2, more reliable but with the same problem

What to do?

Draken
  • 3,134
  • 13
  • 34
  • 54
z10h22
  • 51
  • 1
  • 9
  • 1
    Possible duplicate of [How to Wait for windows process to finish before opening file in java](http://stackoverflow.com/questions/8334532/how-to-wait-for-windows-process-to-finish-before-opening-file-in-java) – huellif Sep 13 '16 at 07:06
  • This does not work with image file opened in windows photo viewer – z10h22 Sep 13 '16 at 14:43

1 Answers1

0

Think solution found, I report it for others with same needs:

  1. Instead of use file=new File(), I have used file=File.createTempFile(basename,extension) [you can use apache common IO to manage file name] to create a temporarily file
  2. set file to be deleted on software exit (not on current frame exit) file.deleteOnExit()
  3. try to open file with default programm try{Desktop.getDesktop().open(file)}
  4. catch exception to handle the case no default programm exists, in this case the software open file directory highlighting the file
    catch(IOException e){Runtime.getRuntime().exec("explorer.exe/select," +file.getAbsolutePath()); }
z10h22
  • 51
  • 1
  • 9