How to open the default web browser using java


Description:
This you can be do two way 
  1. java.awt.Desktop class use (Use the isDesktopSupported() method to determine whether the Desktop API is available. On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false.)
  2. Runtime.getRuntime() method use.


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


Previous
Next Post »