Contents Previous Next Index

Appendix   B


This Appendix contains an example that demonstrates the use of the SATSA-JCRMI API.

Preparing the Java Card Platform Simulator

JCRMIMIDlet connects to an example card applet that is distributed with the Java Card Development Kit. If you’ve already worked through the APDUMIDlet example, note that JCRMIMIDlet uses the same demo2 card applets as APDUMIDlet. The first step is to create a card EEPROM image that contains the required applets.

To do this, run the card simulator, specifying that its EEPROM image should be saved. In this example, the file name is demo2.eeprom:

start cref -o demo2.eeprom 

In DOS, start launches cref in a separate window. The next step is to load some of the Java Card Development Kit example applets onto the card. This example assumes that the Java Card Development Kit is installed in \java_card_kit-2_2_1.

apdutool /java_card_kit-2_2_1/samples/src/demo/demo2.scr 

After this step is complete, cref exits, saving its EEPROM image in the file you specified. You only need to perform this step once.

When you have the EEPROM image file, run cref as follows:

start cref -p 9025 -i demo2.eeprom 

When you run JCRMIMIDlet, the SATSA RI is able to find the simulated card that is running the demo2 card applets.

Source Code

The source code for this example is contained in two files, and com\sun\javacard\samples\RMIDemo\ Use the jcrmic tool, described in Chapter 4, to generate a stub for the remote interface Purse.

JCRMIMIDlet Source Code

 * Copyright © 2004 Sun Microsystems, Inc.  All rights reserved. 
 * Use is subject to license terms. 
import javax.microedition.jcrmi.JavaCardRMIConnection; 
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 
import com.sun.javacard.samples.RMIDemo.Purse; 
public class JCRMIMIDlet 
    extends MIDlet 
    implements CommandListener, Runnable { 
  private final String kRMIURL = "jcrmi:0;AID=a0."; 
  private JavaCardRMIConnection mConnection; 
  private Display mDisplay; 
  private Form mMainForm; 
  private Command mExitCommand, mGoCommand, mBackCommand; 
  private Form mProgressForm; 
  public JCRMIMIDlet() { 
    mExitCommand = new Command("Exit", Command.EXIT, 0); 
    mGoCommand = new Command("Go", Command.SCREEN, 0); 
    mBackCommand = new Command("Back", Command.BACK, 0); 
    mMainForm = new Form("JCRMI Example"); 
    mMainForm.append("Press Go to use the SATSA-JCRMI API " + 
        "to connect to a card application."); 
  public void startApp() { 
    mDisplay = Display.getDisplay(this); 
  public void pauseApp() {} 
  public void destroyApp(boolean unconditional) {} 
  public void commandAction(Command c, Displayable s) { 
    if (c == mExitCommand) { 
    else if (c == mGoCommand) { 
      mProgressForm = new Form("Working..."); 
      Thread t = new Thread(this); 
    else if (c == mBackCommand) { 
  public void run() { 
    try { 
      setProgress("Opening a connection"); 
      mConnection = (JavaCardRMIConnection); 
      setProgress("Getting an initial reference"); 
      Purse purse = (Purse)mConnection.getInitialReference(); 
      short balance = purse.getBalance(); 
      setProgress("Balance = " + balance); 
      setProgress("Crediting 20");; 
      setProgress("Debiting 15"); 
      balance = purse.getBalance(); 
      setProgress("Balance = " + balance); 
      setProgress("Setting account number to 54321"); 
      purse.setAccountNumber(new byte[] {5, 4, 3, 2, 1}); 
      setProgress("Closing connection"); 
    catch (Exception e) { 
      try { mConnection.close(); } catch (Throwable t) {} 
      Form f = new Form("Exception"); 
  private void setProgress(String s) { 
    StringItem si = new StringItem(null, s); 
    si.setLayout(Item.LAYOUT_2 | Item.LAYOUT_NEWLINE_AFTER); 

Purse Source Code

 * Copyright © 2002 Sun Microsystems, Inc. All rights reserved. 
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
package com.sun.javacard.samples.RMIDemo; 
import java.rmi.*; 
import javacard.framework.*; 
public interface Purse extends Remote { 
  public static final short UNDERFLOW = (short)0x6000; 
  public static final short OVERFLOW  = (short)0x6001; 
  public static final short BAD_ARGUMENT  = (short)0x6002; 
  public static final short MAX_AMOUNT = (short)400; // for whatever reason 
  public short getBalance() throws RemoteException; 
  public void debit(short m) throws RemoteException, UserException; 
  public void credit(short m) throws RemoteException, UserException; 
  public void setAccountNumber(byte[] number)  
      throws RemoteException, UserException; 
  public byte[] getAccountNumber() throws RemoteException; 


Contents Previous Next Index SATSA Developer's Guide
SATSA Reference Implementation 1.0