Forum Stats

  • 3,827,462 Users
  • 2,260,780 Discussions
  • 7,897,252 Comments

Discussions

Programming Client Side Javacard App

839553
839553 Member Posts: 2
edited Feb 15, 2011 1:14PM in Java Card
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.
Tagged:

Answers

  • safarmer
    safarmer Member Posts: 2,829
    Repost with code tags for readability

    ====================================================================================

    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<em>);<br />
    System.out.println();<br />
    }<br />
    <br />
    }
    <pre class="jive-pre"><code class="jive-code">Thanks in advance.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • safarmer
    safarmer Member Posts: 2,829
    Hi,

    You need to add the jcrmiclientframework.jar to your projects build path for these classes. It is in ${JC_HOME}/lib.

    Cheers,
    Shane
  • 839553
    839553 Member Posts: 2
    Sorry about the code Shane,
    I will be more carefully in the future,
    but I don't know how to add these TAGS,
    is there any option to <Quote> my code well formatted?

    I will try the solution,
    Thank you a lot.
  • tschodt
    tschodt Member Posts: 537
    You can use
    [&#8203;code]
        line 1
        line 2
    [&#8203;/code]
    or
    {&#8203;code}
        line 1
        line 2
    {&#8203;code}
    (for some reason the latter does not handle indentation on the first line)
This discussion has been closed.