Skip to content

Instantly share code, notes, and snippets.

@devbhuwan
Last active August 5, 2016 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbhuwan/9a1ae729ab733fa5639e073c1f0a7676 to your computer and use it in GitHub Desktop.
Save devbhuwan/9a1ae729ab733fa5639e073c1f0a7676 to your computer and use it in GitHub Desktop.
How to launch associated applications registered on the native desktop to handle link or a file in Java
import java.awt.Desktop;
import java.io.File;
import java.net.URI;
/**
*
* @author Bhuwan Prasad Upadhyay
*/
public class LaunchAppInNativeOSToHandleLinkAndFile {
public static void main(String[] args) throws Exception {
LaunchAppInNativeOSToHandleLinkAndFile handleLinkAndFile
= new LaunchAppInNativeOSToHandleLinkAndFile();
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
handleLinkAndFile.lauchUserDefaultBrowser(desktop);
Thread.sleep(500);
handleLinkAndFile.lauchUserDefaultMailClient(desktop);
Thread.sleep(500);
handleLinkAndFile.lauchUserDefaultFileOpenApplication(desktop);
Thread.sleep(500);
handleLinkAndFile.lauchUserDefaultFileEditApplication(desktop);
Thread.sleep(500);
handleLinkAndFile.lauchUserDefaultFilePrintApplication(desktop);
} else {
throw new UnsupportedOperationException("Desktop Environment Not Supported");
}
}
/*
To Launch User Default Browser [eg. chrome/firefox/IE]
*/
public void lauchUserDefaultBrowser(Desktop desktop) throws Exception {
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(new URI("http://www.google.com"));
System.out.println("#lauchUserDefaultBrowser()->Browser Opened");
}
}
/*
To Launch User Default Mail Client [eg. outlook]
*/
public void lauchUserDefaultMailClient(Desktop desktop) throws Exception {
if (desktop.isSupported(Desktop.Action.MAIL)) {
desktop.mail();
System.out.println("#lauchUserDefaultMailClient()->Mail Client Opened");
}
}
/*
To Launch User Default File Open Application [eg. notepad]
*/
public void lauchUserDefaultFileOpenApplication(Desktop desktop) throws Exception {
if (desktop.isSupported(Desktop.Action.OPEN)) {
desktop.open(File.createTempFile("LaunchAppInNativeOSToHandleLinkAndFile"
+ System.currentTimeMillis(), ".txt"));
System.out.println("#lauchUserDefaultFileOpenApplication()->File Open Application Opened");
}
}
/*
To Launch User Default File Edit Application [eg. notepad]
*/
public void lauchUserDefaultFileEditApplication(Desktop desktop) throws Exception {
if (desktop.isSupported(Desktop.Action.EDIT)) {
desktop.edit(File.createTempFile("LaunchAppInNativeOSToHandleLinkAndFile"
+ System.currentTimeMillis(), ".txt"));
System.out.println("#lauchUserDefaultFileEditApplication()->File Edit Application Opened");
}
}
/*
To Launch User Default File Print Application [eg. notepad]
*/
public void lauchUserDefaultFilePrintApplication(Desktop desktop) throws Exception {
if (desktop.isSupported(Desktop.Action.PRINT)) {
desktop.print(File.createTempFile("LaunchAppInNativeOSToHandleLinkAndFile"
+ System.currentTimeMillis(), ".txt"));
System.out.println("#lauchUserDefaultFilePrintApplication()->File Print Application Opened");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment