Jun 11

Many developers use the toURL() method of the File class to convert a File to a URL. This method however does not handle correctly escaping characters that are illegal in URL’s (such as spaces) and has been deprecated.

To convert a File to a URL you should instead first use the toURI() method to convert the file path to a URI. The URI class has a toURL() method that can then be used to convert the URI to a URL.

File file = new File(path);
URI uri = file.toURI();
URL url = uri.toURL();

// or alternatively in one line

URL url = file.toURI().toURL();

written by objects \\ tags: , , ,

Oct 03

The Desktop class was added in Java 6 to handle launching associated applications on the native desktop.

Following shows how to launch the default mail application and open a mail composing window.

Desktop desktop = Desktop.getDesktop();
desktop.mail();

Or you can use a mailto: URI to also fill out the address field.

Desktop desktop = Desktop.getDesktop();
desktop.mail(new URI("mailto:someone@acme.com"));

written by objects \\ tags: , , , , ,

Oct 03

The Desktop class was added in Java 6 to handle launching associated applications on the native desktop.

Following shows how to launch a browser to display a URI.

URI uri = new URI("http://www.objects.com.au");
Desktop desktop = Desktop.getDesktop();
desktop.browse(uri);

written by objects \\ tags: , , , ,