Java AWT | Desktop Class

23 Oct, 2019
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file.
Important Points About Desktop Class :
It can open a default web browser showing a specific URI
It can launch default mail client with optional mailto URI
It can launch a registered application to open, edit or print a specific file.
Different Methods in Desktop Class
Below programs illustrate the Desktop class in Java AWT:
Program to Launch the browser and open a specific URI// Java Program to Launch the browser// and open a specific URIimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;          // Main Method     public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a URI            URI u = new URI("www.geeksforgeeks.org");              Desktop d = Desktop.getDesktop();            d.browse(u);        }        catch (Exception evt) {        }    }}Output :
// Java Program to Launch the browser// and open a specific URIimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;          // Main Method     public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a URI            URI u = new URI("www.geeksforgeeks.org");              Desktop d = Desktop.getDesktop();            d.browse(u);        }        catch (Exception evt) {        }    }}
Output :
Program to Launch the mail to a specific URI// Java Program to Launch the// mail to a specific URIimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {            URI u = new URI("mailto:contribute@geeksforgeeks.org");              Desktop d = Desktop.getDesktop();            d.mail(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}Output:
// Java Program to Launch the// mail to a specific URIimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {            URI u = new URI("mailto:contribute@geeksforgeeks.org");              Desktop d = Desktop.getDesktop();            d.mail(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}
Output:
Program to open a file// Java Program to open a fileimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;class desk extends JFrame implements ActionListener {    // frame    static JFrame f;           // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\image.png");              Desktop d = Desktop.getDesktop();            d.open(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}Output :
// Java Program to open a fileimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;class desk extends JFrame implements ActionListener {    // frame    static JFrame f;           // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\image.png");              Desktop d = Desktop.getDesktop();            d.open(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}
Output :
Program to open a file for editing// Java Program to open a file for editingimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\sample.txt");              Desktop d = Desktop.getDesktop();            d.edit(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}Output :
// Java Program to open a file for editingimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\sample.txt");              Desktop d = Desktop.getDesktop();            d.edit(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}
Output :
Program to open a file for printing// Java Program to open a file for printingimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\sample.txt");              Desktop d = Desktop.getDesktop();            d.print(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}Output :
// Java Program to open a file for printingimport java.awt.*;import javax.swing.*;import java.awt.event.*;import java.io.*;import java.net.*;  class desk extends JFrame implements ActionListener {      // frame    static JFrame f;      // Main Method    public static void main(String args[])    {        desk d = new desk();          // create a frame        f = new JFrame("desktop");          // create a panel        JPanel p = new JPanel();          // create a button        JButton b = new JButton("launch");          // add Action Listener        b.addActionListener(d);          p.add(b);        f.add(p);        f.show();        f.setSize(200, 200);    }      // if button is pressed    public void actionPerformed(ActionEvent e)    {        try {              // create a file            File u = new File("f:\\sample.txt");              Desktop d = Desktop.getDesktop();            d.print(u);        }        catch (Exception evt) {            JOptionPane.showMessageDialog(this, evt.getMessage());        }    }}
Output :
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html
ManasChhabra2
Java-AWT
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.