Skip to Main Content

Java Card

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Programming Client Side Javacard App

839553Feb 14 2011 — edited Feb 15 2011
Hi everybody! I'm a software developer working in a Java card Project, but I'm completely new in the JavaCard environment. I need some help to solve a pair of problems that got me stuck. First of all, I found some sample programs that I am following (javacard kit 2.2.2). Particulary, I'm trying to run the Purse RMI example. I've built the Javacard applets without problems and run a simulation on JCWDE. Now I understand something about APDU commands. But, in the Client side I found a problem. There are some "import" statements that my Java IDE doesn't find.

In particular ( Red ball msg: "com.sun.javacard.clientlib does not exist"):

import com.sun.javacard.clientlib.*;
import com.sun.javacard.rmiclientlib.*;

Please, let me know if I need any additional Tool or library to install.
I am using:

- java_card_kit-2_2_2
- IDE: Eclipse (with plugins) and NetBeans (6.9.1)
- jdk1.5.0_16

==========
CODE EXAMPLE
==========
package rmiclient;

import java.rmi.*;
import javacard.framework.*;

import com.sun.javacard.clientlib.*;
import com.sun.javacard.rmiclientlib.*;

import java.util.ResourceBundle;

public class AppletRMIClient {

private static final byte[] RMI_DEMO_AID = {
(byte)0xa0, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x62, (byte)0x03,
(byte)0x01, (byte)0xc, (byte)0x8,
(byte)0x01
};

public static void main(String[] argv) throws RemoteException{

ResourceBundle msg
= ResourceBundle.getBundle("com/sun/javacard/clientsamples/purseclient/MessagesBundle");

CardAccessor ca = null;

try {

// open and powerup the card
ca = new ApduIOCardAccessor();

// create a "filter" for RMI protocol
JCRMIConnect jcRMI = new JCRMIConnect(ca);

// select the Java Card applet
if(argv.length == 0) {
jcRMI.selectApplet( RMI_DEMO_AID, JCRMIConnect.REF_WITH_CLASS_NAME );
}
else {
jcRMI.selectApplet( RMI_DEMO_AID, JCRMIConnect.REF_WITH_INTERFACE_NAMES );
}

// obtain the initial reference
System.out.print(msg.getString("msg01")+" ");
Purse myPurse = (Purse) jcRMI.getInitialReference();
if(myPurse != null) {
System.out.println(msg.getString("msg02"));
}
else {
throw new Exception(msg.getString("msg03"));
}

// get the balance amount
System.out.print(msg.getString("msg04"));
short balance = myPurse.getBalance();
System.out.println(msg.getString("msg05") + balance); // prints 0

System.out.println(msg.getString("msg06"));
myPurse.credit((short)20);
System.out.println(msg.getString("msg07"));
myPurse.debit((short)15);

System.out.print(msg.getString("msg08"));
balance = myPurse.getBalance();
System.out.println(msg.getString("msg05") + balance); // prints 5

System.out.println(msg.getString("msg09"));
myPurse.setAccountNumber(new byte[]{5,4,3,2,1}); // expecting OK

System.out.print(msg.getString("msg10"));
byte[] acct_number = myPurse.getAccountNumber();
printArray(acct_number); // prints 5 4 3 2 1

System.out.println(msg.getString("msg11"));
myPurse.setAccountNumber(new byte[]{6,7,8,9,10,11});

}
catch(UserException e) {
System.out.println(msg.getString("msg12") + e.toString());
System.out.println(msg.getString("msg13") + Integer.toHexString(0x00FFFF & e.getReason()));
}
catch (Exception e){
System.out.println(e);
}
finally {
try{
if(ca!=null){
ca.closeCard();
}
}
catch (Exception ignore){
// System.out.println(ignore);
}
}
}

private static void printArray(byte[] arr) {
for(int i=0; i<arr.length; ++i) System.out.print(" " + arr);
System.out.println();
}

}



Thanks in advance.

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Mar 15 2011
Added on Feb 14 2011
4 comments
263 views