The desktop class in Java

Source: Internet
Author: User
Tags mail

A new class--desktop has been added after Jdk1.6, which is explained in the JDK as follows:

The Desktop class allows a Java application to launch associated applications in the registered native to Desktop a URI or a file.

Supported operations include:

Launching the User-default browser to show a specified URI;

Launching the User-default mail client with a optional mailto URI;

Launching a registered application to open, edit or print a specified file.

The meaning of this passage is:

The desktop class allows one Java application to start another local application to process a URI or file request, which includes several methods as follows:

1. Start the user's default browser to display the specified URI link

2. Start the user's default mail client send the message specified by the URI

3. Start a registered application (locally installed application) to open, edit or print a specified file

Below, give a test code to illustrate the function and use of this class, with comments in the code:

Package com.brucezhang.desktop;  
Import Java.awt.Desktop;  
Import Java.io.File;  
      

Import Java.net.URI;  
      
    public class Desktoptest {private static Desktop Desktop; Open Web page with default browser public static void browse () {if (desktop.isdesktopsupported ()) {Desktop = De  
            Sktop.getdesktop ();

The try {//uri Specifies the address of the Web page desktop.browse (The New URI ("Http://blog.csdn.net/dlutbrucezhang?  
            Viewmode=contents "));  
            catch (Exception e) {//Todo:handle Exception e.printstacktrace ();  }}//edit file public static void edit () {if (desktop.isdesktopsupported ())  
            {desktop = Desktop.getdesktop ();  
            try {desktop.edit (new File ("D:\\brucezhang.txt")); catch (Exception e) {//Todo:handle Exception E.PRintstacktrace (); The file is opened, similar to the procedure for editing the file, to see the display of the file public static void open () {if deskt  
            Op.isdesktopsupported ()) {desktop = Desktop.getdesktop ();  
            try {desktop.open (new File ("D:\\brucezhang.txt"));  
            catch (Exception e) {//Todo:handle Exception e.printstacktrace (); The specified file public static void print () {if (desktop.isdesktopsupport  
            Ed ()) {desktop = Desktop.getdesktop ();  
            try {desktop.print (new File ("D:\\brucezhang.txt"));  
            catch (Exception e) {//Todo:handle Exception e.printstacktrace ();  
        }}/** * @param args */public static void main (string[] args) { TODO Auto-generated Method STub browse ();  
        Edit ();  
        Open ();  
    Print (); }  
      
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.