Home arrow Java arrow Page 4 - Deploying Java Applets
JAVA

Deploying Java Applets


In this second part of a three-part series on using Java with deployment frameworks, you will learn how to deploy applets in a self-contained manner, and what they are and are not typically permitted to do once they are downloaded. This article is excerpted from chapter two of Advanced Java Game Programming, written by David Wallace Croft (Apress; ISBN: 1590591232).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 20
April 17, 2008
TABLE OF CONTENTS:
  1. · Deploying Java Applets
  2. · Deploying as an Executable JAR
  3. · Deploying with Java Web Start
  4. · Accessing the Default Browser
  5. · Dynamic Linking Using Reflection

print this article
SEARCH DEVARTICLES

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.

 


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials