Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Java Tip 66: Control browsers from your Java application - JavaWorld

Java Tip 66: Control browsers from your Java application - JavaWorld

Tutorial Details:

Java Tip 66: Control browsers from your Java application
Java Tip 66: Control browsers from your Java application
By: By Steven Spencer
Tired of using Java HTML widgets that don't quite work? Use this simple class to control your system's native browser
It's great that Java applets and browsers are so tightly integrated, but what if you want to have your Java application display a URL? There's no API call in any Java package that can help you with that.
However, using the exec() command, you can fork a process and issue a command to the underlying OS. The only problem is figuring out just which command needs to be issued to control the browsers on each platform.
On Unix, for Netscape, it was easy to figure this out as you only need to type "netscape -help". If Netscape is already running, the command is this:
netscape -remote openURL(http://www.javaworld.com)
And, if the browser is not already running, you type:
netscape http://www.javaworld.com
Under Windows, it took much exploration and a bit of luck to find something equivalent that wouldn't open a new browser windows for each request. This command, in fact, works better then the Unix command, as you don't have to know whether or not the browser is already running, and the command invokes the default browser -- it is not hard-coded to run a Netscape browser. If Microsoft's Internet Explorer is your default browser, then this will dislay the page in Internet Explorer. To display a page, issue the following command (you can try this in a DOS Shell): rundll32 url.dll,FileProtocolHandler http://www.javaworld.com
Follow-up Tip!
from Ryan Stevens: For Mac users, here's an easy way to open a Web page in the default browser, usi ng MRJ 2.2:
import com.apple.mrj.MRJFileUtils;
import java.io.*;
class Open {
String url = "http://www.yourpage.com/";
public static void main(String[] args) { new
Open(); }
Open() {
try {
MRJFileUtils.openURL(url);
} catch (IOException ex) {}
}
}
from Mark Weakly:
You can launch the command line stuff on Mac similar to Unix (MacOS 8 and 9), except you must place the command-line tokens into a java.lang.String array. The array gets passed to the process exec() method. For example:
String[] commandLine = { "netscape", "http://www.javaworld.com/" };
Process process = Runtime.getRuntime().exec(commandLine);
The BrowserControl code
The class I have written, called BrowserControl takes the above into account and will work for both Windows and Unix platforms. For the Unix platform, you must have Netscape installed and in your path in order for this to work unmodified. If you're a Mac user and know how to invoke a browser from within a Java application, let me know...
To display a page in your default browser, just call the following method from your application:
BrowserControl.displayURL("http://www.javaworld.com")
Note: You must include the URL protocol ("http://" or "file://").
Here is the code for BrowserControl.java :
import java.io.IOException;
/**
* A simple, static class to display a URL in the system browser.
*
* Under Unix, the system browser is hard-coded to be 'netscape'.
* Netscape must be in your PATH for this to work. This has been
* tested with the following platforms: AIX, HP-UX and Solaris.
*
* Under Windows, this will bring up the default browser under windows,
* usually either Netscape or Microsoft IE. The default browser is
* determined by the OS. This has been tested under Windows 95/98/NT.
*
* Examples:
*
BrowserControl.displayURL("http://www.javaworld.com")
*
BrowserControl.displayURL("file://c:\\docs\\index.html")
*
BrowserContorl.displayURL("file:///user/joe/index.html");
*
* Note - you must include the url type -- either "http://" or
* "file://".
*/
public class BrowserControl
{
/**
* Display a file in the system browser. If you want to display a
* file, you must include the absolute path name.
*
* @param url the file's url (the url must start with either "http://"
or
* "file://").
*/
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" +
cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch(IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
/**
* Try to determine whether this application is running under Windows
* or some other platform by examing the "os.name" property.
*
* @return true if this application is running under a Windows OS
*/
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
/**
* Simple example.
*/
public static void main(String[] args)
{
displayURL("http://www.javaworld.com");
}
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
}
Conclusion
BrowserControl is a class that allows you to control your system's native browser from any Java application. By using a native browser to display HTML, you get more complete support and faster rendering of HTML, while reducing the amount of code you have to write. And, your users have less to learn, as they are likely already familiar with their system's browser.
Submit your favorite tip
We would like to pass on your Java tips to the rest of the Java world. For detailed guidelines on what we're looking for in a Java Tip and how to ensure that your tip has all the necessary elements, see the Java Tips Index page?and be sure to read the Java Tips Author Guidelines carefully.
Once you've read the guidelines, go ahead and write up your coolest tips and tricks, and send them to tips at javaworld.com . You may find yourself an author in JavaWorld with your helpful hints chosen as the next Java Tip !
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Java Tip 66: Control browsers from your Java application - JavaWorld

View Tutorial:
Java Tip 66: Control browsers from your Java application - JavaWorld

Related Tutorials:

Displaying 1 - 50 of about 4357 Related Tutorials.

Showing an image in Tool Tip
Image Tool Tip,Java Image Tool Tip Example,Showing an Image in Tool Tip,Code... Tip           ...;   This section illustrates how to show an image in tool tip
 
Java Enabled browsers
Java Enabled browsers Java Enabled browsers... application. Today most of the web browser are java compatible. Few of them... of the browsers today are java enabled. There are various ways to check whether
 
Java Enabled browsers
Java Enabled browsers Java Enabled browsers... java enabled. Mostly used java enabled web browsers are Internet... java enabled or not. Most of the currently using browsers are java enabled
 
Setting Tool Tip Text for items in a JList Component
in the JList component of the Java Swing. Tool Tip text is the help text of any component... tip text for items present in the JList component in Java Swing. In this program... is imported from the javax.swing.plaf.multi.*; package of Java. This method locate
 
Application Servers for Java
these application servers to test and deploy your Java bases web and enterprise... Application Servers for Java,Application Servers in Java Application Servers for Java    
 
Java Get Month from Date
the current month from the current date using Java Application Language.  You... Java Get Month from Date Java Get Month from.... That means if you want to show the date in your application then you'll have
 
Installing Sun Java System Application Server Platform Edition
Installing Sun Java System Application Server Platform Edition Installing Sun Java System Application Server Platform Edition... Application Server Platform Edition on your windows machine. Download the latest
 
Open Source Application Server
development by releasing code for the popular Java System Application Server Platform... to sell with its Enterprise Linux 3 platform. Commercial application servers from...) under the Open Source licence. The XML Application Server is written in Java
 
Developing JSP, Java and Configuration for Hello World Application
Writing JSP, Java and Configuration for Hello World Application,Struts 2..." into the lib directory of your web application. Here is the output of ant... description on how Struts 2 Hello World Application works: Your browser sends
 
Beginners Java Tutorial
the switch statement in your java program for developing java application.  ... Java application can accept any number of arguments directly from the command... language. This tutorial is for beginners, who wants to learn Java from scratch
 
Execute SQL Queries with Java Application
Execute SQL Queries with Java Application Execute SQL Queries with Java Application     ... program to connect java application and execute queries like create table in mysql
 
Tutorial - Sun Java System Application Server Platform Edition
and Application Migration tools to migrate your applications from other Application... sun java system application server platform,sun java system application server platform 9 Sun Java System Application Server
 
Java Virtual Machine
to understand how it's possible for a Java applet/application to run on many... Machine. The Java Virtual Machine Most compilers translate from the source... is from Java source to Java Virtual Machine (JVM) machine code. This may seem
 
Master Java In A Week
as a programming language Java is an Object oriented application... into HTML format from Java source code. It is interesting to know...; Java Empowered Browsers Java language is the most powerful language and is widely
 
Java server Faces Books
applications for years. The technology builds on the experience gained from Java... on the experience gained from Java Servlets, JavaServer Pages, and numerous... is currently being distributed with the Glassfish Java EE 5 application server (also
 
Java Swing
Application. The frame in java works like the main window where your... and you can use it in your program. Java Swing tutorials first gives you brief... components within an application and between Java and native applications
 
Installing Java
to develop a new Java application? What browser the required to run my applet? What tools are to be used to develop a java application? Is there any... the tools to develop the java application. Let's start with the Java Development
 
How Struts Works
without writing much java code inside your jsp. These tags are used create...;       The basic purpose of the Java Servlets in struts is to handle requests made by the client or by web browsers
 
Java Example Codes and Tutorials
Introduction to Java Applet and example explaining how to write your first Applet. Java... Java is an Object oriented application programming language developed... released the Java SE 6 on Monday December 11. So go and grab your copy. 
 
Java Swing Tutorials
Application. The frame in java works like the main window where your... and you can use it in your program. Java Swing tutorials first gives you brief... components within an application and between Java and native applications
 
Java Interpreter
and the Internet Explorer are Java enabled. This means that these browsers...-line arguments needed for the application as well: % java [interpreter options... the application by executing that method. From there, the application starts additional
 
Java Interpreter
and the Internet Explorer are Java enabled. This means that these browsers contain...-line arguments needed for the application as well: % java [interpreter options... the application by executing that method. From there, the application starts additional
 
Jobs Tip
JOB Tips Jobs Tip    ... with Your Resume  Every job skills coach worth his salt stresses the importance of your resume- they tell you that your resume can make or break your
 
Applet
also run this example from your favorite java enabled browser... Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet
 
Java Interpreter
Explorer are Java enabled. This means that these browsers contain Java interpreter. With the help of this Java interpreter we download the Applets from... needed for the application as well: % java [interpreter options] class name
 
Java Swing Tutorials
Application. The frame in java works like the main window where your... and you can use it in your program. Java Swing tutorials first gives you brief... components within an application and between Java and native applications
 
XML Interviews Question page10
on what facilities your users' browsers implement. XML is about describing... was predefined and hardwired into browsers. In XML, where you can define your own tagset... language to transform your XML into HTML?which browsers, of course, already know how
 
Simple JSF Hello Application
can create this file also by your own or copy from other JSF Application... will definitely form a basis for you to develop  your own JSF application with more... web.xml file from WEB-INF folder of any other application available in TOMCAT
 
Java error reading from file
java error reading from file Java error reading from file         ... In this Tutorial we help you in understanding a n example from Java error reading
 
New to Java?
operating system without recompiling your source code. Java technology is based... merely from desktop to the resource of the web. It contains JVM and Java... in compiling, running, debugging and documenting the application, making the Java
 
New to Java?
operating system without recompiling your source code. Java technology is based... merely from desktop to the resource of the web. It contains JVM and Java... in compiling, running, debugging and documenting the application, making the Java
 
Display Data from Database in JSP
application with mysql database and execute query to display data from the specified table... Display Data from Database in JSP Display Data from Database in JSP         
 
Java Software
Java Software You need sofware to develop your Java programs. You can use... A compiler (usually Sun's JDK). A way to run your programs from within the IDE... - Software/Java Development Kit This is mainly a compiler. Download from
 
Beginners Java Tutorial
the switch statement in your java program for developing java application.  ... Java application can accept any number of arguments directly from the command... language. This tutorial is for beginners, who wants to learn Java from scratch
 
Shifting from Ant to Maven
of abstraction above Ant (and uses Jelly). Maven can be used to build any Java application... Shifting from Ant to Maven Shifting from Ant...;   Maven is entirely a different creature from Ant
 
Java Swing Tutorials
Application. The frame in java works like the main window where your... and you can use it in your program. Java Swing tutorials first gives you brief... components within an application and between Java and native applications
 
Java Programmers with Financial Application
Java Programmers with Financial Application Java Programmers with Financial Application     ...;         Position Vacant: Java
 
Controlling your program
statement that the break statement exit control from the loop but continue... Controlling your program Controlling your program... takes place from top to bottom. We will learn how the different kinds of statement
 
JAVA JAZZ UP - Free online Java magazine
online testing service for Java ME application: ?Sony Ericsson Virtual... features of Java language, which makes a java application internationalized... of the key features of Java language, which makes a java application
 
Downloading and Installing Apache Geronimo Application Server
v2.1 is Java EE 5 Certified application server and it requires J2SE 1.5 or above... Geronimo Application server can be downloaded from its official site at http... Application Server Downloading and Installing Apache
 
Java review
Compilation/Execution Steps Edit source (.java) program (eg, your IDE) Compile source... and statics). Start JVM (Java Virtual Machine) at main (application). JIT... version of the Java run-time system may recompile from byte code to machine
 
Animating Images in Java Application
in Java Swing Application Animating Images in Java Application          ... by one from the list of the images. Images are added to the label on the frame
 
Applet versus Application
. In this application you will see how to import packages in your application. Java libraries... Applet versus Java Application,Difference Between Applet and Application... the java.applet.Applet class while the java applications start execution from
 
Web Application Directory Structure:
Web Application Directory Structure,Servlet Directory Structure,Java Web Application Directory Structure Web Application...;    To develop an application using  servlet or jsp make
 
Web Application Directory Structure:
Web Application Directory Structure,Servlet Directory Structure,Java Web Application Directory Structure Web Application...;    To develop an application using  servlet or jsp make
 
Apache Geronimo Application server Tutorial
Application Download the sample helloworld application from following URI. http... popular open source build tool for Java based applications. Download ant from http... JavaEE( or J2EE, old name) application server. It is so much capable that it can
 
Learn Java Quickly, Quick Java Tutorial
the function of JVM How to write your First Java program Ok, now you know...), then its time to write your first Java program. Create a source code program... for every Java application. So first define the class name and lets take
 
Accessing Database from servlets through JDBC!
develop an web application in windows machine running Java web... servlets are supported by a number of web servers. Java Web Server from... . In order to compile your servlet you also need Java Servlet
 
Changing Look and Feel of Swing Application
Swing Application. The look and feel feature of Java Swing provides more... Changing Look and Feel,Change Swing Look and Feel,Java Swing Look and Feel Example Changing Look and Feel of Swing Application
 
Learn Java - Learn Java Quickly
the function of JVM How to write your First Java program Ok, now you know...), then its time to write your first Java program. Create a source code program... for every Java application. So first define the class name and lets take
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.