Christmas is coming soon! To thank for the support from all our dear customers, Christmas promotional activity is going on in JavaCardOS online store. For more activities details, please check this post.

Java Card Applet Development Guide ——Applet Demo

Applets Development Guide

Moderator: product

User avatar
product
Posts: 94
Joined: Wed May 20, 2015 2:04 am
Points :418
Contact:

Java Card Applet Development Guide ——Applet Demo

Post by product » Mon Aug 31, 2015 10:51 pm

This shows you how to create the famous "Hello World" application.

Code: Select all

//define package name.
package appletDemo;

//import using java card API interface.
import javacard.framework.*;


/*
 * Package: appletDemo
 * Filename: appletDemo.java
 * Class: appletDemo
 */
public class appletDemo extends Applet
{
   //Define the value of CLA/INS in APDU, you can also define P1, P2.
   private static final byte CLA_DEMO_TEST            = (byte)0xB0;
    private static final byte INS_DEMO_TMP1            = (byte)0x11;
    private static final byte INS_DEMO_TMP2            = (byte)0x12;
   
   //Create an instance of the Applet subclass using its constructor, and to register the instance.
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      //Only one applet instance can be successfully registered each time the JCRE calls the Applet.install method.
      new appletDemo().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }
   //Process the command APDU, All APDUs are received by the JCRE and preprocessed.
   public void process(APDU apdu)
   {
      //Select the Applet, through the select method, this applet is selectable, After successful selection, all APDUs are delivered to the currently selected applet via the process method.
      if (selectingApplet())
      {
         return;
      }
      //Get the APDU buffer byte array.
      byte[] buf = apdu.getBuffer();
      //Calling this method indicates that this APDU has incoming data.
      apdu.setIncomingAndReceive();
      
      //If the CLA is not equal to 0xB0(CLA_DEMO_TEST),  throw an exception.
      if(buf[ISO7816.OFFSET_CLA] != CLA_DEMO_TEST)
      {
         ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
      }
      //Dispatch INS in APDU.
      switch (buf[ISO7816.OFFSET_INS])
      {
         //The APDU format can be "B0 11 P1 P2 Lc Data Le", such as "B0110000" or "B01101020311223300".
      case INS_DEMO_TMP1:
         SendData(apdu);
         break;
         //The APDU format can be "B0 12 P1 P2 Lc Data Le", such as "B0125566" or "B012000002112200".
      case INS_DEMO_TMP2:
         //Set 0x5555 into the 'buf' array,  the offset is 0.
         Util.setShort(buf, (short)0, (short)0x5555);
         //Send the first 2 bytes data in 'buf', the hex of JCRE sending data is "55 55 90 00".
         apdu.setOutgoingAndSend((short)0, (short)2);
         break;
      default:
         //If you don't know the INS, throw an exception.
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }
   
   //Define a function named 'SendData'
   private void SendData(APDU apdu)
   {
      //Get the apdu buffer datas again.
      byte [] buffer = apdu.getBuffer();
      
      //Define a byte string with the message "Hello World"
      byte sendStr[] = {'H','E','L','L','O',' ','W','O','R','L','D'};
      short len = (short) sendStr.length;
      //Copy "Hello world" character to APDU Buffer.
      Util.arrayCopyNonAtomic(sendStr, (short)0, buffer, (short)0, (short)len);
         
      //Set the data transfer direction to outbound.
      apdu.setOutgoing();
      //Set the actual length of response data.
      apdu.setOutgoingLength(len);
      //Sends the data of APDU buffer 'buf', the length is 'len' bytes,  the offset is 0.
      apdu.sendBytes((short) 0, (short)len);
   }

}



You can also download it .
You do not have the required permissions to view the files attached to this post. Please login first.

User avatar
Ellisun
Posts: 47
Joined: Wed May 20, 2015 3:47 am
Points :440
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by Ellisun » Wed Dec 02, 2015 2:48 am

A very basic and interesting helloworld applet for java card beginners. :) :)

predators
Posts: 21
Joined: Wed Aug 19, 2015 7:18 am
Points :12
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by predators » Mon Dec 07, 2015 11:05 pm

Ellisun wrote:A very basic and interesting helloworld applet for java card beginners. :) :)


Yes, the comments of code are very detailed.

edejongh
Posts: 2
Joined: Wed Jan 25, 2017 4:46 am
Points :28
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by edejongh » Mon Feb 06, 2017 12:38 pm

Hi all, how do I test/call this class from the debugger or the pyApdutool?

I would like to invoke the send method?

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3027
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by UNKNwYSHSA » Wed Feb 15, 2017 6:03 am

edejongh wrote:Hi all, how do I test/call this class from the debugger or the pyApdutool?

I would like to invoke the send method?


Debugger (JCIDE):
1 Launch debugger
2 /select <Applet AID>
3 /send B011000000

pyApduTool:
1 Connect to the card
2 Send APDU: 00A40400<LC><Applet AID>
3 Send APDU: B011000000
sense and simplicity

edejongh
Posts: 2
Joined: Wed Jan 25, 2017 4:46 am
Points :28
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by edejongh » Wed Feb 15, 2017 10:42 am

Thanks for your response. I figured it out, but it really wasn't pretty as it's virtually impossible to debug and playing with HEX is just painful. I am now focusing on the JC 3connected edition and it's a whole world better. No RMIC stub compilation nightmares and much easier to test and demo.

Anyone know what percentage of JC 3 enabled SIM's are out ther? Just a ball park figure?

regards

ed

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3027
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by UNKNwYSHSA » Wed Feb 15, 2017 10:00 pm

What is the "RMIC"?
and
"Anyone know what percentage of JC 3 enabled SIM's are out ther? Just a ball park figure?" ??
sense and simplicity

dongho1492
Posts: 5
Joined: Thu Jul 19, 2018 2:30 am
Points :80
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by dongho1492 » Mon Jul 23, 2018 10:37 pm

Hello.
I am studying Java card technology fundamentally, and thanks to this article, I really know a lot. Again, thank you.

dongho1492
Posts: 5
Joined: Thu Jul 19, 2018 2:30 am
Points :80
Contact:

Re: Java Card Applet Development Guide ——Applet Demo

Post by dongho1492 » Mon Jul 23, 2018 10:41 pm

Hello.
I am studying Java card technology fundamentally, and thanks to this article, I really know a lot. Again, thank you.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 1 guest

JavaCard OS : Disclaimer