Description: |
This you can be do two way
|
Code 1 using Desktop browse : |
package com.demo;
import java.awt.Desktop;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class OpenUrl {
private static String[] urlArray = {
"/2016/11/how-to-convert-xml-to-object-and-object.html",
"/2016/04/how-to-include-google-map-in-your.html",
"/2016/04/clone-under-standing.html",
"/2016/04/custom-binary-tree-printer.html",
"/2016/11/public-key-encryption-and-private-key.html"};
private static List<String> createUrl() {
int size = urlArray.length;
List<String> url=new LinkedList<String>();
for (int i=0; i<size; i++)
{
String s="http://java2blogs.blogspot.com"+urlArray[i];
url.add(s);
}
return url;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
Desktop desktop = java.awt.Desktop.getDesktop();
List<String> urlString = createUrl();
Random rand = new Random();
while (true) {
int choice = rand.nextInt(urlString.size());
URI oURL = new URI(urlString.get(choice));
desktop.browse(oURL);
Thread.sleep(20000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Code 2 Open Browser using Windows or linux: |
package com.demo;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class OpenUrl {
private static void osCheck(String url){
String os = System.getProperty("os.name").toLowerCase();
Runtime rt = Runtime.getRuntime();
try {
if (os.indexOf("win") >= 0) {
rt.exec("rundll32 url.dll,FileProtocolHandler " + url);
} else if (os.indexOf("mac") >= 0) {
rt.exec("open " + url);
} else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0) {
String[] browsers = { "epiphany", "firefox", "mozilla",
"konqueror", "netscape", "opera", "links", "lynx" };
StringBuffer cmd = new StringBuffer();
for (int i = 0; i < browsers.length; i++)
cmd.append((i == 0 ? "" : " || ") + browsers[i] + " \""
+ url + "\" ");
rt.exec(new String[] { "sh", "-c", cmd.toString() });
} else {
return;
}
} catch (Exception e) {
return;
}
return;
}
private static String[] urlArray = {
"/2016/11/how-to-convert-xml-to-object-and-object.html",
"/2016/04/how-to-include-google-map-in-your.html",
"/2016/04/clone-under-standing.html",
"/2016/04/custom-binary-tree-printer.html",
"/2016/11/public-key-encryption-and-private-key.html"};
private static List<String> createUrl() {
int size = urlArray.length;
List<String> url=new LinkedList<String>();
for (int i=0; i<size; i++)
{
String s="http://java2blogs.blogspot.com"+urlArray[i];
url.add(s);
}
return url;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
List<String> urlString = createUrl();
Random rand = new Random();
while (true) {
int choice = rand.nextInt(urlString.size());
osCheck(urlString.get(choice));
Thread.sleep(20000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Out put: |
both out put are same
|