Skillzone Skillzone - 22 days ago 8
Java Question

Java URL open and Close after random time

I need code that will open url and close it after some random time(3-10minutes)

Something like:

Desktop d = Desktop.getDesktop();
d.browse(new URI("http://google.pl"));
d.wait(1000);
d.destroy(); //error there is no destroy function here


any ideas?

Answer

I looked into this some more, the Desktop class does not have a close() feature to it.

If you want to launch another program via Java you should use the Process class. Doing something similar to this:

import java.io.IOException;
public class Process {
    public static void main(String[] args) {
        try {
            Runtime runTime = Runtime.getRuntime();
            Process process = runTime.exec("notepad");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}