I have an application which consist of menu's.In the help menu i have tutorial menuitem.What i want to do is when i click the menuitem "tutorial" a document in the application directory need to be opened.Can i acheive this?
Asked
Active
Viewed 108 times
3 Answers
3
It depends on the document. You could give the Desktop
class a try. From the docs: "The Desktop class allows a Java application to launch associated applications registered on the native desktop to handle a URI or a file.". Note that you need at least java 6 for this.
If that doesn't work, you could also use one of the various exec(...)
methods from the Runtime
class. But it has quite a bit of pitfalls, this document describes them: When Runtime.exec() won't.

Bart Kiers
- 166,582
- 36
- 299
- 288
2
You can use java.awt.Desktop.getDesktop().open(file)
method.

MockerTim
- 2,475
- 1
- 25
- 31
-
Hi i am referring tutorial.doc in the file.But when the application is runned on mac the extension with doc doesn't exist.How can i overcome this – hemanth kumar Jul 01 '11 at 09:06
-
@hemanth You'd better try to avoid using `.doc` files if you want MAC support. For example, you can convert doc to HTML. See [convert-doc-docx-to-semantic-html](http://stackoverflow.com/questions/1335412/convert-doc-docx-to-semantic-html) for details. – MockerTim Jul 01 '11 at 10:40
-
i have diagrams in my document can i still convert my doc to html? – hemanth kumar Jul 01 '11 at 12:32
-
hey sorry about the question, mac supports the .doc files. – hemanth kumar Jul 01 '11 at 12:41
-
@hemanth Would you mind to give me the link to the source of your info about .doc support. I'm still not acquainted with mac os. – MockerTim Jul 01 '11 at 12:50
-
@hemanth Yep. You can use [java.io.File.setReadOnly()](http://download.oracle.com/javase/6/docs/api/java/io/File.html#setReadOnly()). – MockerTim Jul 04 '11 at 08:37
1
You should use the Desktop class from the Java standard Library to let the OS launch the proper application to view the document. Call the open
method to view the document.

Mathias Schwarz
- 7,099
- 23
- 28