0

I have searched online and couldn't find a straight answer. I know how to read a file into java to be read or manipulated or even how to delete a file but I don't know how to execute an external file (stored on the user's system).

For instance if I had a list of books and their locations on the user's system. I'd like when the user clicks on a particular button then a pdf or document in a particular location is opened up in the relevant software e.g. adobe document reader or word etc.

How do I do this?

James Kingsbery
  • 7,298
  • 2
  • 38
  • 67
  • You mean that you are using a kind of FileChooser and when the user select the file you want to open it? `Desktop.getDesktop().open(myFile);` this may help – nachokk Feb 10 '14 at 17:29
  • 2
    possible duplicate of [Execute external program in java](http://stackoverflow.com/questions/13991007/execute-external-program-in-java) – dev_feed Feb 10 '14 at 17:30
  • @dev_feed, I think he doesn't want to execute a file but open using default associate program (such as described in Desktop.open() method). But, sometimes... http://stackoverflow.com/questions/18004150/desktop-api-is-not-supported-on-the-current-platform. – rdllopes Feb 10 '14 at 17:46
  • @rdllopes my mistake, I read the title and whole first paragraph as a duplicate. hopefully someone with a better understanding of the question will edit. – dev_feed Feb 10 '14 at 17:52
  • possible duplicate of [Execute external program from Java](http://stackoverflow.com/questions/2874591/execute-external-program-from-java) – james.garriss Feb 10 '14 at 20:50
  • @james-kingsbery, I think this title is confusing people. Please, reconsider your title edition. – rdllopes Feb 11 '14 at 15:35

1 Answers1

8

You can use Desktop class.

the Desktop API (java.awt.Desktop) API, which allows Java applications to interact with default applications associated with specific file types on the host platform.

Read more here How to integrate with the desktop class

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • 1
    @mikemil yes and also it's platform dependant that's why you have to use `isDesktopSupported()` as oracle tutorial says. – nachokk Feb 10 '14 at 18:22