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?

6
  • 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 Commented Feb 10, 2014 at 17:29
  • 2
    possible duplicate of Execute external program in java Commented Feb 10, 2014 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... stackoverflow.com/questions/18004150/…. Commented Feb 10, 2014 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. Commented Feb 10, 2014 at 17:52
  • possible duplicate of Execute external program from Java Commented Feb 10, 2014 at 20:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@mikemil yes and also it's platform dependant that's why you have to use isDesktopSupported() as oracle tutorial says.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.