Calling URL Browser From Java Application

This program teaches you how you can call the browser in your OS to open the URL that you are passing from your Java application. This program is fully compiled and tested.

package com.kushal.utilities;

 * @author Kushal Paudyal
 * Last Modified On: 26th August, 2008
import java.lang.reflect.Method;

import javax.swing.JOptionPane;

public class OpenDefaultBrowserFromJava {
	private static final String errMsg = "ERROR WHILE ATTEMPTEING TO LUNCH WEB BROWSER";
	private static final String errFindingBrowser="COULD NOT FIND THE BROWSER";
	public static void main(String args [])
		String urlToOpen="";

	 * This method takes URL as a parameter and tries to
	 * identify Operating system and then lunches the broswer
	 * with the given URL
	public static void openURL(String url) {
		String operatingSystem = System.getProperty("");
		try {
			 * Check if it is windows OS 
			if (operatingSystem.startsWith("Windows"))
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
			 * If not windows OS, Check if it is a Mac OS
			else if (operatingSystem.startsWith("Mac OS")) {
				Class fileManager = Class.forName("");
				Method openURL = fileManager.getDeclaredMethod("openURL",
						new Class[] { String.class });
				openURL.invoke(null, new Object[] { url });
			 * Or, it might be some unix or linux OS
			else { 
				 * There are different browsers possible.
				String[] browsers = { 	"firefox", 
				String browser = null;
				for (int count = 0; count < browsers.length && browser == null; count++)
					if (Runtime.getRuntime().exec(
							new String[] { "which", browsers[count] })
							.waitFor() == 0)
						browser = browsers[count];

				if (browser == null) 
					throw new Exception(errFindingBrowser);
					Runtime.getRuntime().exec(new String[] { browser, url });

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, errMsg + ":n" + e.getMessage());


[My Video Promotion]
  1. How to extract plain Text from HTML Website easily in Java
  2. How To Find What Java Version You Are Using?
  3. Computing the total, free and usable disk space easily using JDK 1.6
  4. Creating and java.util.sql objects from Calendar object
  5. Bar Code Generation In Java Using iText API
  6. Simple Tag Generator In Java For Music Blogs
  7. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  8. Calling URL Browser From Java Application
  9. Java Reflection Tutorial – List Methods Of A Class
  10. Cleaning Punctuation And White Spaces From A String
  11. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  12. Prime Number Finder In Java
  13. Utility Class For Getting SQL Connection and Closing It
  14. Java Design Pattern – Singleton Pattern
  15. Reading / Writing File in Java and String Manipulation
  16. How to capture a screenshot using Java and save it to a file?
  17. Calculating The Difference Between Two Dates In Terms of Months
  18. Getting The Name Of Currently Logged In User In The System
  19. How To Set And Get System Clipboard Contents In Java
  20. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  21. Java Reflections Tutorial
  22. Generating Nepali Unicode Sequence For Characters
  23. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  24. Replacing non-ASCII characters using Java Regular Expressions
  25. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    } else {
    return false;
    public long getTotal()
    return total;

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    return counter.getTotal();

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.