Wednesday, February 17, 2010

Java Tips: How to open a file with default editor from Java applications

How to open a file with default editor from Java applications

For a long time we couldn't easily use Java applications to open the default (for the operating system) editor application (like notepad in Windows Operating Systems for text files). Starting with JDK 1.6 you can use the java.awt.Desktop class to easily launch the default editor (of your operating system) from Java applications. Read below for more details and the code.

java.net.URI uri = new URI("http://gaeatimes.com");
java.awt.Desktop.getDesktop().edit(new File("Readme"));

The documentation for edit() states: Launches the associated editor application and opens a file for editing.

Note: The EDIT action is not supported in CentOS 5.x systems. Let us know if you have success on your Operating System.

Did you like this tip? Consider subscribing to our site for more such tips.

No comments: