Client Example

Client applications run on a terminal supporting a Java Virtual Machine environment such as Java Platform, Standard Edition or Java Platform, Micro Edition (Java ME).

The PurseClient application interacts with the remote stub classes generated by a stub generation tool and the Java Card platform-specific information managed by the Java Card platform client-side framework located in packages com.sun.javacard.clientlib and com.sun.javacard.rmiclientlib.

The client example below uses standard Java RMIC compiler-generated client-side stubs. The client application as well as the Java Card client-side framework rely on the APDU I/O library for managing and communicating with the card reader and the card on which the Java Card applet PurseApplet resides. This makes the client application very portable on Java SE platforms. See the Java Card 3 Platform Development Kit User Guide, Classic Edition Version 3.0.5 for information on the APDU I/O library.

The following example shows a very simple PurseClient application that is the client application of the Java Card technology-based program PurseApplet:

import examples.purse.*;
import javacard.framework.UserException;

public class PurseClient extends java.lang.Object {
    public static void main(java.lang.String[] argv) {
        // arg[0] contains the debit amount
        short debitAmount = (short) Integer.parseInt(argv[0]);
        CardAccessor ca = null;
        try {
            // open and powerup the card
            ca = new ApduIOCardAccessor();
            // create an RMI connector instance for the Java Card platform
            JCRMIConnect jcRMI = new JCRMIConnect(ca);
            byte[] appAID = new byte[] {0x01,0x02,0x03,0x04,0x05,0x06,0x07, 0x08};
            // select the Java Card applet
            jcRMI.selectApplet( RMI_DEMO_AID, JCRMIConnect.REF_WITH_CLASS_NAME );
            or
            jcRMI.selectApplet( RMI_DEMO_AID, JCRMIConnect.REF_WITH_INTERFACE_NAMES );
            // obtain the initial reference to the Purse interface
            Purse myPurse = (Purse) jcRMI.getInitialReference();
            // debit the requested amount
            try {
                short balance = myPurse.debit ( debitAmount );
            }catch ( UserException jce ) {
                short reasonCode = jce.getReason();


                // process UserException reason information
             }
             // display the balance to user
         }catch (Exception e) {
             e.printStackTrace();
         }finally {
             try {
                 if(ca!=null){
                     ca.closeCard();
                 }
             }catch (Exception e) {
                 e.printStackTrace();
             }
         }
     }
}