Home | Download | PDF | API | FAQ | Search | Feedback | History

> ## Creating the Application Client

> An application client is a program written in the Java programming language. At runtime, the client program executes in a different virtual machine than the Application Server. For detailed information on the `appclient` command-line tool, see the man page at `http://java.sun.com/javaee/5/docs/relnotes/cliref/index.html`.

> The application client in this example requires two JAR files. The first JAR file is for the Java EE component of the client. This JAR file contains the client's deployment descriptor and class files; it is created when you run the New Application Client wizard. Defined by the Java EE Specification, this JAR file is portable across all compliant application servers.

> The second JAR file contains all the classes that are required by the client program at runtime. These classes enable the client to access the enterprise beans that are running in the Application Server. The JAR file is retrieved before you run the application. Because this retrieved JAR file is not covered by the Java EE specification, it is implementation-specific, intended only for the Application Server.

> The application client source code is in the `ConverterClient.java` file, which is in this directory:

>     <INSTALL>/javaeetutorial5/examples/ejb/converter/converter-app-client/src/java

> You compiled this code along with the enterprise bean code in the section Compiling and Packaging converter.

> ### Coding the Application Client

> The `ConverterClient.java` source code illustrates the basic tasks performed by the client of an enterprise bean:

> - Creating an enterprise bean instance
> - Invoking a business method

> #### Creating a Reference to an Enterprise Bean Instance

> Java EE application clients refer to enterprise bean instances by annotating static fields with the `@EJB` annotation. The annotated static field represents the enterprise bean's business interface, which will resolve to the session bean instance when the application client container injects the resource references at runtime.

>     @EJB
>     private static Converter converter;

> The field is static because the client class runs in a static context.

> #### Invoking a Business Method

> Calling a business method is easy: you simply invoke the method on the injected `Converter` object. The EJB container will invoke the corresponding method on the `ConverterBean` instance that is running on the server. The client invokes the `dollarToYen` business method in the following lines of code.

>     BigDecimal param = new BigDecimal ("100.00");
>     BigDecimal amount = currencyConverter.dollarToYen(param);

> #### ConverterClient Source Code

> The full source code for the `ConverterClient` program follows.

>     package com.sun.tutorial.javaee.ejb;

>     import java.math.BigDecimal;
>     import javax.ejb.EJB;

>     public class ConverterClient {
>       @EJB
>       private static Converter converter;

>       public ConverterClient(String[] args) {
>       }

>       public static void main(String[] args) {
>         ConverterClient client = new ConverterClient(args);
>         client.doConversion();
>       }

>       public void doConversion() {
>         try {
>           BigDecimal param = new BigDecimal("100.00");
>           BigDecimal yenAmount = converter.dollarToYen(param);

>           System.out.println("$" + param + " is " + yenAmount
>               + " Yen.");
>           BigDecimal euroAmount = converter.yenToEuro(yenAmount);
>           System.out.println(yenAmount + " Yen is " + euroAmount
>               + " Euro.");

>           System.exit(0);
>         } catch (Exception ex) {
>           System.err.println("Caught an unexpected exception!");
>           ex.printStackTrace();
>         }
>       }
>     }

> ### Compiling the Application Client

> The application client files are compiled at the same time as the enterprise bean files, as described in Compiling and Packaging converter.