Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Blog
    • Java
    • Programming Tips
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to open a URL in Java | 100% Perfect for Beginners

April 30, 2022 - Updated on March 9, 2023
in Java
Reading Time: 5 mins read
0
How to open a URL in Java
1.1k
VIEWS
Share on FacebookShare on TwitterShare via Email

When a developer is learning more about programming and comes up with the idea of putting a URL in the application and is wondering how to open a URL in Java using the computer’s default browser, you can search the internet for answers to your questions about how to implement this feature in your application. This tutorial is perfect for your project.

Contents

Toggle
  • How to open a URL in Java using the default browser
  • Methods and descriptions in the Desktop class
  • Examples
    • How to open a URL in Java using the browse() method
      • Output
      • Youtube Video
    • How to use the edit() method
      • Output
    • How to use the mail() method
      • Output
    • How to use the open() method
      • Output
    • How to use the print() method
      • Output

How to open a URL in Java using the default browser

When working on a Java project that requires to open a URL in Java, it’s important to understand how to use the Desktop class to achieve this. The Desktop class in Java provides a way for your program to interact with the computer system by opening files and launching other applications. This makes it a powerful tool for performing various tasks from within a Java program.

To open a URL in Java using the Desktop class in Java, you first need to import the java.awt.Desktop package. This package contains the Desktop class, which has several methods you can use to interact with the computer system. One of the most important methods is the browse(URI uri) method, which opens a URL in Java using the default web browser.

Another advantage of the Desktop class is its ability to open the default mail client application and launch the supported application to open, edit, and print a specific file. This makes it useful for performing various tasks, such as sending emails and opening documents, from within a Java program.

To use the Desktop class, you can create an instance of the class using the getDesktop() method, which returns the Desktop object associated with the current system. You can then call the desired method on the Desktop object to perform the task you need.

Methods and descriptions in the Desktop class

There are nine valuable methods in the Desktop class in Java. These methods will help you open the default application using a Java program on your computer.

  1. browse(URI uri) – This method is used to open a URL in Java using the default browser to display a URI or website in your default web browser.
  2. edit(File file) – This method is used to launch the default text editor application like notepad to edit a text file.
  3. getDesktop() – This method is used to return the Desktop instance of the current browser.
  4. isDesktopSupported() – This method is used to identify whether this class is supported on the computer
  5. isSupported(Desktop.Action action) – This method is used to identify if the action is supported on the computer.
  6. mail() – This method is used to open the default mail client application on your computer.
  7. mail(URI mailToURI) – This method is used to open the default mail client on your computer with a specified email.
  8. open(File file) – This method is used to open a file like a java class file or a text file on your computer.
  9. print(File file) – This method is used to print a file with the desktop printing facility.

Let’s proceed to the examples below, and I will provide examples of each method above. The first one is an example of how to open a URL in Java using the default browser, and the following methods according to the lists above. Please proceed below to learn examples of how to use it.

Examples

How to open a URL in Java using the browse() method

To open a URL in Java is very easy. You can simply import the Desktop class and use the browse() method in the Desktop class. So, the browse() method is used to open a URL in Java using the default browser. Using this method, you can open whatever URL you want from your Java program, but it depends on whether it is supported. If the desktop is not supported, we can use another method to open the URL.

// URL opener method
private void urlOpener(String url) {
    // Get the Desktop instance of the computer
    Desktop desktop = Desktop.getDesktop();
    try {
        // checks if the desktop action is supported
        if (desktop.isSupported(Desktop.Action.OPEN)) {
            // if supported, open the URL using the browse() method
            desktop.browse(java.net.URI.create(url));
        } else {
            // if the desktop is not supported, proceed to other method
            // using the command
            Process p;
            try {
                p = Runtime.getRuntime().exec("cmd /c start " + url);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Output

How to open a URL in Java

Youtube Video

YouTube video

How to use the edit() method

To open and edit a file in Java is easy too. We can use the edit() method in the java.awt.Desktop class. This method’s primary goal is to run the appropriate program to modify the file. Please proceed to the example below to learn more about implementing this code in your Java program.

private void desktopOpener(String f) throws IOException {
    Desktop desktop = Desktop.getDesktop();
    if (desktop.isSupported(Desktop.Action.EDIT)) {
        File file = new File("C:\\Users\\kensoft\\Desktop\\"+f);
        desktop.edit(file);
    }
}

Output

Java awt Desktop

How to use the mail() method

If you have implemented to open an email from your Java application, this mail() method is perfect to use to open the email directly to your default mail client application. Please proceed to the example below to learn more about implementing this code in your Java program.

 private void desktopOpener(String email){
    try{
        URI uri = new URI("mailto:"+email);
        Desktop desktop = Desktop.getDesktop();
        desktop.mail(uri);
    }catch(IOException | URISyntaxException e){
        System.out.println(e);
    }
}

Output

Java awt Desktop mail

How to use the open() method

To open a file in Java using the Desktop class is easy. For example, you want your Java application to have the ability to open files on the computer system, such as images, videos, etc. This method is the right one to open a file from your Java program. Please proceed to the example below to learn more about implementing this code in your Java program.

private void desktopOpener(String openFile){
    try{
        File file = new File("C:\\Users\\kenth\\Desktop\\"+openFile);
        Desktop desktop = Desktop.getDesktop();
        desktop.open(file);
    }catch(IOException e){
        System.out.println(e);
    }
}

Output

Java awt desktop open

How to use the print() method

The print() method is used to print the file from the computer using your Java application to perform the action. Please proceed to the example below to learn more about implementing this code in your Java program.

private void desktopOpener(String openFile){
    try{
        File file = new File("C:\\Users\\kensoft\\Desktop\\"+openFile);
        Desktop desktop = Desktop.getDesktop();
        desktop.print(file);
    }catch(IOException e){
        System.out.println(e);
    }
}

Output

Java awt Desktop print

Previous Post

Working with layout in JavaFX | 100% Perfect for beginners

Next Post

How to use the Group in JavaFX| 100% Perfect for beginners

KENSOFT

KENSOFT

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Related tutorials

How to Use the JavaFX Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
212
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
220
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
646
Next Post
Layout in JavaFX

How to use the Group in JavaFX| 100% Perfect for beginners

Layout in JavaFX

JavaFX Pane Layout Tutorial | 100% Perfect for beginners

Layout in JavaFX

HBox in JavaFX Tutorial With Code | 100% Perfect Tutorial

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025
top 10 common coding mistakes beginner make

Top 10 Common Coding Mistakes Beginners Make

June 29, 2025
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

My name is Kent, and KENSOFT represents a combination of my name and my passion for software development. Java is my preferred programming language, and I specialize in developing computer applications using this technology.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

C# vs Java vs Python

C# vs Java vs Python: Which Programming Language to Learn

July 2, 2025
Complete Guide to Freelance Programming in 2025

Complete Guide to Freelance Programming in 2025

July 1, 2025 - Updated on July 3, 2025
Best IntelliJ IDEA Plugins for Developers in 2025

Best IntelliJ IDEA Plugins for Developers in 2025

June 30, 2025

© 2025 Made With Love By KENSOFT PH

No Result
View All Result
  • Blog
    • Java
    • Programming Tips
  • Download
    • Kenshot
  • Contact
  • About
  • Java Quiz

© 2025 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.