opening internet browser in java program

opening internet browser in java program

View Answers

April 4, 2009 at 12:30 PM

Hi Omkar,

Use the following code:

1) 'OpenBrowser.java'
import javax.swing.*;
import java.lang.reflect.Method;

public class OpenBrowser {
public static void openURL(String url) {
String osName = System.getProperty("os.name");
try {
if (osName.startsWith("Windows"))
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
else {
String[] browsers = {"firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
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];
Runtime.getRuntime().exec(new String[] {browser, url});
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage());
}
}
}

2) 'Browser.java'
import javax.swing.*;
import java.awt.event.*;

public class Browser {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
final JTextField url =new JTextField(20);
JButton button = new JButton("Open Browser");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OpenBrowser.openURL(url.getText().trim()); }
} );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(new JLabel("URL:"));
panel.add(url);
panel.add(button);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}

}

Thanks









Related Tutorials/Questions & Answers:
opening internet browser in java program - Swing AWT
opening internet browser in java program  i want a code to open the internet browser in swing program using JFrame.  Hi Omkar, Use...) { JOptionPane.showMessageDialog(null, "Error in opening browser" + ":\n" + e.getLocalizedMessage
Opening a browser only with body from a java program
Opening a browser only with body from a java program  Hi all, I got following requirement, Opening a browser window only with body and title bar. And based on some condition, after some time, i have to close the browser window
Advertisements
opening new browser with new JSESSIONID using java
opening new browser with new JSESSIONID using java  I am facing following problem, I am trying to open a new browser using java. First i have opened one IE browser and manually. And i ran my LaunchURL.java file, it is opening new
Opening files with .jnlp extension on browser!?!?!
Opening files with .jnlp extension on browser!?!?!  I working... for Runtime.getRuntime().exec("..........."); I came across the java code.... Rather i want to open it in my browser(Mozilla/IE) itself. Kindly provide me
Delete Internet Explorer History in java program
Delete Internet Explorer History in java program  code for Delete Internet Explorer History in java program
Java - Opening a URL from an Applet
Java - Opening a URL from an Applet  ... document or browser window. In this program you will see that many features have been used for opening url from an applet. This program is using two functions
Java - Opening a url in new window from an applet
Java - Opening a url in new window from an applet       This is the example of opening a url from an applet. This program shows that how a url is opened in a new document or browser
Help with opening a text file upon closing a program.
Help with opening a text file upon closing a program.  I need some helping in figuring out something I want to do. I have this program... that file to open whenever the user exits the program. My code so far
Opening IE from Java Swing Application in Kiosk mode
Opening IE from Java Swing Application in Kiosk mode  Hi All, I want...) { String browserPath = "C:/Program Files/Internet Explorer...(); } } }   Here is a simple example of opening browser
Java for Internet Explorer
browser as Internet Explorer 7. To enable or disable Java in Internet Explorer 4... Java for Internet Explorer      ... and select Internet Options. Click on the Programs tab. Click Manage Add-ons
problem in swing program for opening two windows with same login credentials
problem in swing program for opening two windows with same login credentials  I Face two problems while writing the code in swing program 1.i developed one application using swings that has username,password when i login
opening new window - Java Beginners
opening new window  Hi All, I have two classes with me , lets say A and B.when I press a button in class A , the class B should open in a new window.i.e., I want to know, how a new window(B) can be opened by clicking a button
Error Opening File - Java Interview Questions
Error Opening File  Dear, Please if help me to resolve this problem. I have an application in java that works correctly under Eclipse... this generated file, a message was displayed: Error opening file. But If I
finding java code on internet
finding java code on internet  Is there anybody to help me? i want to some java coding web sites.it is prerequest for me
Java Web Browser Problem
Java Web Browser Problem  Hello When run my web browser...."); setTitle("NextVision Browser"); setSize(600,400); Container...); objMenu.add(objMenuExit); JMenuItem Internet=new JMenuItem
Applet in Java
Applet is a Java program designed for execution within the web browser. They are embedded into HTML pages and run on Java enabled web browsers like Mozilla... the Internet and run them. An Applet is a Java subclass that extends
JAVA SCRIPT CODE FOR OPENING A WEBSITE IN A NEW WINDOW - Java Beginners
JAVA SCRIPT CODE FOR OPENING A WEBSITE IN A NEW WINDOW   PLS TELL ME? HOW CAN I DEFINE A WINDOW.OPEN FUNCTION WITHIN ANOTHER FUNCTION e.g IF THERE ARE THREE FIELDS OF A FORM, AND AFTER CKECKING THAT ALL THE FIELDS HAVING
browser
browser  how to make a browser
Opening Notepad
Opening Notepad  Sir/madam I want to open notepad from java applications and save the contents on it. Can someone please help me to do...}; Runtime.getRuntime().exec(commands); }   Java Create
Browser making by java coding
Browser making by java coding  import javax.swing.*; import...*; import java.io.*; public class Browser extends JFrame implements...[]) { if(args.length==0) new Browser ("http:google.com"#; else new Browser#args#0
java code to get the internet explorer version
java code to get the internet explorer version  Hi, Could u plz help me in finding the internet explorer version using java code...i have the javascript but i want this in java code... Thanks, Pawan
Toolbar to a browser - Java Beginners
to display toolbar on the browser? Thanks  I want to create a website's... browser, some site ask us to download the application and install it. so i wanted
browser
in the swing because I am working on my project Browser and one more problem is that when I entered the URL then some page open which made in java and the page
Java Swing Open Browser
Java Swing Open Browser Java provides different methods to develop useful... that will open the specified url on the browser using java swing. This example will really... (Exception e) { JOptionPane.showMessageDialog(null, "Error in opening browser
INTERNET - CVS
in the toolbar. ? Select What to Clear -> Browser History item. ? Check the Delete Cookies option. ? Select the browser(s). You can remove cookies for several browsers simultaneously. In Internet Explorer even if you remove cookies
how to get browser cookies in java
how to get browser cookies in java  Hi, How to get cookies in serverlet and then print? how to get browser cookies in java? Thanks   Hi, Use following code: Cookie[] cookies = request.getCookies(); for (int i = 0
How to download and save a file from Internet using Java?
How to download and save a file from Internet using Java?  How to download and save a file from Internet using Java
browser
browser  how to add menu items in the menu in java swing through java coding
ModuleNotFoundError: No module named 'opening_hours'
ModuleNotFoundError: No module named 'opening_hours'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'opening_hours' How to remove the ModuleNotFoundError: No module named
Java Program
Java Program  A Simple program of JSP to Display message, with steps to execute that program   Hi Friend, Follow these steps: 1)Go... and press enter. 9)You jsp program will then display the output on the browser
Browser tire - Java Interview Questions
Browser tire  If we use multithreading in my application then which tire is needed on clients browser so that application run successfully? Plz help me ass soon as possible
Java - Opening a url in new window from an applet
Java - Opening a url in new window from an applet  ... in this program to open any url by replacing the value of url. In this example our applet will open new browser window and then show you the web page. Opening a new
Java program to get domain name by URL
Java program to get domain name by URL       We can also get the domain name by the java program. Domain name are used to represent the symbolic representation
java program for
java program for   java program for printing documents,images and cards
Embed Default Browser within Java Application
Embed Default Browser within Java Application  After some googling i... to embed browser with or without using NetbeansADS_TO_REPLACE_1 I ve already tried this 2 minutes Browser But i m getting the error in netbeans
Cross platform browser question - Java Beginners
Cross platform browser question  Can you tell me what this is specifially asking for? I have my program that works fine, but the instructor said...-browser (works in Firefox and MS IE) web page using anonymous JS methods
a Java program
a Java program    Write a Java program to print even numbers from 2 to 1024? Write a Java program to print ? My Name is Mirza? 100 times? Write a Java program to print Fibonacci Series? Write a Java program to reverse a number
Java Program
Java Program  A Java Program that print the data on the printer but buttons not to be printed
java program
java program  write a program to print 1234 567 89 10
Opening a URL from an Applet
document or browser window.ADS_TO_REPLACE_1 In this program you will see that many features have been used for opening url from an applet. This program is using two... Opening a URL from an Applet     
java program
java program  Write a program to demonstrate the concept of various possible exceptions arising in a Java Program and the ways to handle them.  ... in Java
java program
java program  write java program for constructor,overriding,overriding,exception handling
java program
java program  how to write an addition program in java without using arithematic operator
java program
java program  write a java program to display array list and calculate the average of given array
java program
java program  write a java program to display array list and calculate the average of given array
Java Program
Java Program  java program to insert row in excel sheet after identifying an object
java program
java program  java program to implement the reflection of a particular class details like constructor,methods and fields with its modifiers
java program
java program  Write a java program to do matrix addition operation On two given matrices
java program
java program  Write a java program to find the number of Positive numbers in m* n matrix
java program
java program  Write a program to create an applet and display The message "welcome to java

Ads