Thursday, October 23, 2014

Launch default web browser for displaying page section/particular position/sub topic like "page.html#namelink" in Java - resolved




import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetDefaultBrowser {

    public static void main(String[] args) {
        System.out.println(new GetDefaultBrowser().getDefaultBrowser());
    }

    public String getDefaultBrowser() {
        String defaultBrowser = "";
        try {
            //1.  Find the default browser in registry
            String browser = "";
            Process process0 = Runtime.getRuntime().exec("REG QUERY HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\http\\UserChoice");
            Scanner kb = new Scanner(process0.getInputStream());
            while(kb.hasNextLine()){
                String line = kb.nextLine();
                if (line.toUpperCase().indexOf("PROGID")>=0){
                    String[] elements = line.split(" ");
                    browser = elements[elements.length-1];
                    System.out.println(browser);
                }
            }

            //2. Get the default browser path from registry
            Process process = Runtime.getRuntime().exec("REG QUERY HKEY_CLASSES_ROOT\\"+browser+"\\shell\\open\\command");
            kb = new Scanner(process.getInputStream());
            while (kb.hasNextLine()) {
                String registry = (kb.nextLine()).replaceAll("\\\\", "/").trim();
                if (registry.indexOf("Default") < 0){
                    continue;
                }
             
                int fidx = registry.indexOf("\"");
                int lidx = registry.indexOf("exe");
             
                if (fidx > 0 && lidx > 0) {
                    defaultBrowser = registry.substring(fidx+1, lidx+3);
                 
                    Runtime.getRuntime().exec(defaultBrowser +
                            " file:///C:/MyFiles/GreatJouney.htm#T01_FLORIDA");
                    break;
                }
            }
         
            kb.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return defaultBrowser;
    }
}

--------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment