Java
  Home arrow Java arrow Page 4 - Deploying Java Applets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Deploying Java Applets
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-04-17

    Table of Contents:
  • Deploying Java Applets
  • Deploying as an Executable JAR
  • Deploying with Java Web Start
  • Accessing the Default Browser
  • Dynamic Linking Using Reflection

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Deploying Java Applets - Accessing the Default Browser


    (Page 4 of 5 )

    Java desktop applications sometimes need to present the user with online web pages for news, documentation, and registration forms. Although Java currently supports the limited ability to display HTML using classjavax.swing.JEditorPane, web browsers such as Netscape or Internet Explorer have additional advanced capabilities such as supporting HTML scripting languages and various multimedia formats. This section describes how to enable your Java desktop applications to launch the default web browser external to your application on the client platform in a platform-independent manner.

    Method showDocument()

    Java has always supported the ability of an applet to control its browser container via theshowDocument()method of interfacejava.applet.AppletContext. Unfortunately, Java desktop applications, as opposed to applets, do not have access to instances ofAppletContext. The JNLP API provides an interface—javax.jnlp.BasicService—which provides a similar method for JNLP desktop applications.

    Isolating Optional Packages

    Assuming a JNLP API implementation is available on the client platform, theshowDocument()method of classBasicServiceautomatically launches the default web browser on the client platform using platform-independent code. However, you will often want to write your game code in such a way that it can still run in an environment where the JNLP libraries are not installed. Ideally, your compiled game code should be able to run as a browser applet, an executable JAR, and as a JNLP Java Web Start application without modification. You can do this by including code that loads the JNLP class libraries, if available on the client machine, or gracefully continue on if not.

      private static JnlpServices createJnlpServices ( )
      //////////////////////////////////////////////////////////////////////
      {
       
    try
        {
          return ( JnlpServices )
            Class.forName ( JnlpServices.IMPL_CLASS_NAME ).newInstance ( );
        }
        catch ( Exception ex )
        {
         
    return null;
        }
        catch ( NoClassDefFoundError er )
        {
          return null;
        }
      }

    The trick to this is to use dynamic class loading. You could also use reflection to accomplish this feat, but I find that as a general rule it is better to use custom interfaces and dynamic class loading. You start by first separating out the code that statically links to the optional package libraryjavax.jnlp. You then attempt to dynamically load that code using
    Class.forName().newInstance(). If JNLP is not installed, the attempt generates aNoClassDefFoundErrorwhich you can then catch and handle gracefully. The preceding code from classJnlpLibin packagecom.croftsoft.core.jnlpdoes just that.

      package com.croftsoft.core.jnlp;

      import java.io.*;
      import java.net.*;

      public interface  JnlpServices
      //////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////////////////////////////////
      {

      public static final String IMPL_CLASS_NAME
        = "com.croftsoft.core.jnlp.JnlpServicesImpl";

      [...]

      public boolean showDocument ( URL url )
        throws UnsupportedOperationException;

    In this case,JnlpServicesis an interface in packagecom.croftsoft.core.jnlpwith no static links to packagejavax.jnlp.IMPL_CLASS_NAMEis the name of a class,JnlpServicesImpl. This class provides a concrete implementation of theJnlpServicesinterface that does require static links to the optional package. A custom interface reference allows your code to handle optional package libraries without static linking and without using reflection.


    CAUTION   In teaching a course on Java game programming, I noted that forgetting to explicitly name dynamically linked classes in the Ant build file was one of the most common causes of grief for my students. This can be difficult to debug as it will not generate a compile-time error and might not generate a runtime error. The calling code might silently ignore a runtime error and carry on under the assumption thatJnlpServicesImplcould not load dynamically because the game is not running within a JNLP client container. The truth of the matter, however, might be thatJnlpServiceImplcould not be loaded because it was not compiled and included in the JAR file.


      public static final JnlpServices JNLP_SERVICES
        = createJnlpServices ( );

      [...]

      public static boolean  showDocument ( URL url )
       
    throws UnsupportedOperationException
      //////////////////////////////////////////////////////////////////////
      {
        check ( );

        return JNLP_SERVICES.showDocument ( url );
      }

      [...]

      private static void  check ( )
       
    throws UnsupportedOperationException
      //////////////////////////////////////////////////////////////////////
      {

        if ( JNLP_SERVICES == null )
        {
          throw new UnsupportedOperationException ( );
        }
      }

    Static methodshowDocument()in classJnlpLib uses the implementation class if it can be dynamically loaded. If not, it throws an
    UnsupportedOperation-Exceptionthat the calling game code can catch and handle gracefully.

      package com.croftsoft.core.jnlp;

      import java.io.*;
      import java.net.*;

      import javax.jnlp.*;

      [...]

      public final class  JnlpServicesImpl
        implements JnlpServices
      //////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////////////////////////////////
      {

      [...]

      public boolean showDocument ( URL url )
        throws UnsupportedOperationException
      //////////////////////////////////////////////////////////////////////
      {
       
    try
        {
          BasicService  basicService = ( BasicService )
            ServiceManager.lookup ( "javax.jnlp.BasicService" );

          return basicService.showDocument ( url );
        }
        catch ( UnavailableServiceException  ex )
        {
         
    throw ( UnsupportedOperationException )
            new UnsupportedOperationException ( ).initCause ( ex );
        }
      }

    When using theshowDocument()method inAppletContext, an instance ofAppletContextis retrieved using the applet instance’s owngetAppletContext()method. An instance of interfaceBasicService, however, is retrieved using the static methodlookup()of classjavax.jnlp.ServiceManager.

    public static Object  lookup ( String name )
      throws UnavailableServiceException;

    If an implementation ofBasicServiceis not available from the JNLP client, theServiceManagerthrows anUnavailableServiceException.JnlpServicesImplconverts this exception from the optional packagejavax.jnlpto an
    Unsupported-OperationExceptionfrom the core packagejava.langif it is thrown. Note that theThrowable.initCause()method is used to attach the original exception to the new exception for examination by the calling code.

     

    More Java Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Advanced Java Game Programming," published...
     

    Buy this book now. This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway