• We can open a website or web page using java.
  • By calling browse() method of java.awt.Desktop.getDesktop() and passing required webpage url as an URI.
  • In order to make URI create a object fro java.net.uri class objet by passing URL of the web page which need to open
  • java.awt.Desktop.getDesktop().browse(uri);
  • Lets see a java program on how to open website url.
                



Program #1: Simple Java Program to open a website or webpage using java.awt.Desktop

  1. package com.instanceofjava.openwebpage;

  2. import java.awt.Desktop;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URI;

  6. public class OpenVLCPlayer {

  7. /**
  8. * @website: www.instanceofjava.com
  9. * @category: how to open a webpage in browser using java code
  10. */
  11.  
  12. public static void main(String[] args)  {
  13. try {
  14. URI uri= new URI("http://www.instanceofjava.com");
  15. java.awt.Desktop.getDesktop().browse(uri);
  16. System.out.println("Web page opened in browser");
  17.  
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }

  22. }

Output:


  1. Web page opened in browser
     

how to open a webpage using java code

Instance Of Java

We are here to help you learn! Feel free to leave your comments and suggestions in the comment section. If you have any doubts, use the search box on the right to find answers. Thank you! 😊
«
Next
Newer Post
»
Previous
Older Post

1 comments for How to open a webpage using java code

Select Menu