Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Quitting the browser

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Quitting the browser

    Hi guys,

    I have this code to open a browser every 5 seconds, but I can't get it to close after 5 seconds.
    Can you guys give me any hints? Thanks a lot!

    import java.awt.Desktop;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;


    public class HelloWorld {
    public static void main(String[] args) throws InterruptedException {

    String url = "www.google.com";

    int x = 5;
    while (x < 8)
    {
    Thread.sleep(5000); // define after how many milliseconds new tab will be opened

    if (Desktop.isDesktopSupported()) {
    Desktop desktop = Desktop.getDesktop();
    try {
    desktop.browse(new URI(url));
    } catch (IOException | URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Thread.sleep(5000);
    //here I want to quit the browser
    }

    } else {
    Runtime runtime = Runtime.getRuntime();
    try {
    runtime.exec("xdg-open " + url);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Thread.sleep(5000);
    }
    }
    }
    }
    }

  2. #2
    Junior Member
    Join Date
    Mar 2020
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Quitting the browser

    Unfortunately it's not possible using the Desktop API since there's no reference object returned from the desktop.browse(...) method.

    A few ideas:

    1. You can try to create and kill the browser process using the Runtime API instead of Desktop.
    2. You can try to call the browse(...) method with a bit of javascript:someFunction(...); in the url.

    Both of these are hacks and I would recommend you look into Selenium WebDriver which was built for this kind of manipulation.

Similar Threads

  1. Replies: 2
    Last Post: August 7th, 2014, 12:46 PM
  2. Replies: 4
    Last Post: April 27th, 2014, 06:40 AM
  3. Web Browser listening
    By badoumba in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 28th, 2013, 09:25 AM
  4. Get information from browser
    By Yoyo_Guru in forum Java Networking
    Replies: 1
    Last Post: July 20th, 2012, 01:54 PM
  5. [SOLVED] Java Browser
    By aman_chauhan in forum AWT / Java Swing
    Replies: 0
    Last Post: July 3rd, 2012, 04:58 AM

Tags for this Thread