9 Replies Latest reply: May 3, 2012 12:40 AM by safarmer RSS

    How can we create a common function for 2 or more java card applets?

    921719
      I have 2 java card applets. They use one common function. But i don't know how to set up it. Anybody knows it, please help me. Please show me step by step to do it. Thank you.
        • 1. Re: How can we create a common function for 2 or more java card applets?
          Umer
          But i don't know how to set up it
          Can you explain your above statement ? I could not figure out what you want to do ...
          • 2. Re: How can we create a common function for 2 or more java card applets?
            921719
            My problem:
            i have 2 applets. i want to create 1 common method that will be used in 2 applets class ( depend on id class, this method will do somthing...).

            My solution:
            I created the new class and put this common method (namely Test_Common.TestMethod(byte idClass)) in to this class. Calls Test_Common.TestMethod(1) from 2 applets class, but when i run Java Card Project, the JCOP Shell returns an error (6A88) (this error made by Test_Common.TestMethod(1) line).

            ==>
            Plz point out for me how can i implement above issue?
            Thanks to let me know if you have any concert
            • 3. Re: How can we create a common function for 2 or more java card applets?
              Umer
              Your approach is right. You need to create a class and create a static method in it and call it from your applets. And without seeing the code nobody can tell you why it is happening.
              • 4. Re: How can we create a common function for 2 or more java card applets?
                921719
                When insert below line of code in the Test_Class_1.java and Test_Class_2, the Project will be failed:

                byte aP1 = Test_common_function.GetP1((byte)0x02);
                byte aP1 = Test_common_function.GetP1((byte)0x01);


                Here is the source code:
                Test_Class_2.java
                 
                package Test_Class_2;
                
                import Test_common_function.Test_common_function;
                import javacard.framework.APDU;
                import javacard.framework.Applet;
                
                public class Test_Class_2 extends Applet
                {
                
                   public static void install(byte[] bArray, short bOffset, byte bLength)
                   {
                      // GP-compliant JavaCard applet registration
                      new Test_Class_2().register(bArray, (short) (bOffset + 1),
                            bArray[bOffset]);
                   }
                
                   public void process(APDU apdu)
                   {
                      byte aP1 = Test_common_function.GetP1((byte)0x02);
                   }
                
                }
                Test_Class_1.java
                 
                package Test_Class_1;
                
                import Test_common_function.Test_common_function;
                import javacard.framework.APDU;
                import javacard.framework.Applet;
                
                public class Test_Class_1 extends Applet
                {
                
                   public static void install(byte[] bArray, short bOffset, byte bLength)
                   {
                      // GP-compliant JavaCard applet registration
                      new Test_Class_1().register(bArray, (short) (bOffset + 1),
                            bArray[bOffset]);
                   }
                
                   public void process(APDU apdu)
                   {
                      byte aP1 = Test_common_function.GetP1((byte)0x02);
                   }
                
                }
                Test_common_function.java
                /**
                 * 
                 */
                package Test_common_function;
                
                import javacard.framework.APDU;
                import javacard.framework.ISO7816;
                import javacard.framework.Applet;
                import javacard.framework.ISOException;
                
                
                public class Test_common_function extends Applet
                {
                   public static void install(byte[] bArray, short bOffset, byte bLength)
                   {
                      // GP-compliant JavaCard applet registration
                      new Test_common_function().register(bArray,
                            (short) (bOffset + 1), bArray[bOffset]);
                   }
                
                   public void process(APDU apdu)
                   {
                      // Good practice: Return 9000 on SELECT
                      if (selectingApplet())
                      {
                         return;
                      }
                
                      byte[] buf = apdu.getBuffer();
                      switch (buf[ISO7816.OFFSET_INS])
                      {
                         case (byte) 0x00:
                            break;
                         default:
                            // good practice: If you don't know the INStruction, say so:
                            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
                      }
                   }
                   
                   public static byte bP1_Class1 = (byte)0x90;
                   public static byte bP1_Class2 = (byte)0x90;
                   public static byte GetP1(byte idclass)
                   {
                      return idclass == (byte)0x01? bP1_Class1:bP1_Class2;
                   }
                   
                   
                }
                and here is the trace log (in case of failure)
                cm>  /term "Remote|localhost:60351"
                --Opening terminal
                
                /card -a a000000003000000 -c com.ibm.jc.CardManager
                resetCard with timeout: 0 (ms) --Waiting for card... ATR=3B F8 13 00 00 81 31 FE 45 4A 43 4F 50 76 32 34    ;.....1.EJCOPv24     31 B7                                              1. IOCTL(). ATR:          T = 1                   => 00 A4 04 00 08 A0 00 00 00 03 00 00 00 00          .............. (578768 nsec) <= 6F 65 84 08 A0 00 00 00 03 00 00 00 A5 59 9F 65    oe...........Y.e     01 FF 9F 6E 06 47 91 01 17 33 00 73 4A 06 07 2A    ...n.G...3.sJ..*     86 48 86 FC 6B 01 60 0C 06 0A 2A 86 48 86 FC 6B    .H..k.`...*.H..k     02 02 01 01 63 09 06 07 2A 86 48 86 FC 6B 03 64    ....c...*.H..k.d     0B 06 09 2A 86 48 86 FC 6B 04 02 15 65 0B 06 09    ...*.H..k...e...     2B 85 10 86 48 64 02 01 03 66 0C 06 0A 2B 06 01    +...Hd...f...+..     04 01 2A 02 6E 01 02 90 00                         ..*.n.... Status: No Error cm>  set-key 255/1/DES-ECB/404142434445464748494a4b4c4d4e4f 255/2/DES-ECB/404142434445464748494a4b4c4d4e4f 255/3/DES-ECB/404142434445464748494a4b4c4d4e4f cm>  init-update 255 => 80 50 00 00 08 37 0D D8 24 C9 C8 32 53 00          .P...7..$..2S. (1829 usec) <= 00 00 DE 81 34 18 39 B7 09 70 FF 02 00 00 3D 02    ....4.9..p....=.     9C 31 C7 89 7A 8E 57 44 05 A7 67 C7 90 00          .1..z.WD..g... Status: No Error cm>  ext-auth plain => 84 82 00 00 10 D8 BE 91 A3 94 E8 34 18 42 28 F6    ...........4.B(.     74 EE 50 06 29                                     t.P.) (1683 usec) <= 90 00                                              .. Status: No Error cm>  delete -r 01020304050102 => 80 E4 00 80 09 4F 07 01 02 03 04 05 01 02 00       .....O......... (833593 nsec) <= 6A 88                                              j. Status: Reference data not found jcshell: Error code: 6a88 (Reference data not found) jcshell: Wrong response APDU: 6A88 Ignoring expected error cm>  delete -r 01020304050103 => 80 E4 00 80 09 4F 07 01 02 03 04 05 01 03 00       .....O......... (769014 nsec) <= 6A 88                                              j. Status: Reference data not found jcshell: Error code: 6a88 (Reference data not found) jcshell: Wrong response APDU: 6A88 Ignoring expected error cm>  delete -r 010203040501 => 80 E4 00 80 08 4F 06 01 02 03 04 05 01 00          .....O........ (734456 nsec) <= 6A 88                                              j. Status: Reference data not found jcshell: Error code: 6a88 (Reference data not found) jcshell: Wrong response APDU: 6A88 Ignoring expected error cm>  upload -c -d -b 250 "D:\WorkSpace\Test_Common_Funcation\bin\Test_common_function\javacard\Test_common_function.cap" => 80 E6 02 00 13 06 01 02 03 04 05 01 08 A0 00 00    ................     00 03 00 00 00 00 00 00 00                         ......... (1864 usec) <= 00 90 00                                           ... Status: No Error Start loading Header.cap (44 byte) => 80 E8 00 00 2C C4 82 03 E3 01 00 25 DE CA FF ED    ....,......%....     02 02 04 00 01 06 01 02 03 04 05 01 14 54 65 73    .............Tes     74 5F 63 6F 6D 6D 6F 6E 5F 66 75 6E 63 74 69 6F    t_common_functio     6E 00                                              n. (1607 usec) <= 00 90 00                                           ... Status: No Error Header.cap loaded (44 byte) Start loading Directory.cap (36 byte) => 80 E8 00 01 24 02 00 21 00 25 00 21 00 0C 00 0B    ....$..!.%.!....     00 26 00 0E 00 4C 00 0C 00 0D 00 00 00 6F 02 5D    .&...L.......o.]     00 02 00 00 00 00 01 01 00 00                      .......... (1368 usec) <= 00 90 00                                           ... Status: No Error Directory.cap loaded (36 byte) Start loading Import.cap (14 byte) => 80 E8 00 02 0E 04 00 0B 01 03 01 07 A0 00 00 00    ................     62 01 01 00                                        b... (1096 usec) <= 00 90 00                                           ... Status: No Error Import.cap loaded (14 byte) Start loading Applet.cap (15 byte) => 80 E8 00 03 0F 03 00 0C 01 08 01 02 03 04 05 01    ................     01 01 00 08 00                                     ..... (1188 usec) <= 00 90 00                                           ... Status: No Error Applet.cap loaded (15 byte) Start loading Class.cap (17 byte) => 80 E8 00 04 11 06 00 0E 00 00 00 80 03 00 FF 00    ................     07 01 00 00 00 1C 00                               ....... (1457 usec) <= 00 90 00                                           ... Status: No Error Class.cap loaded (17 byte) Start loading Method.cap (79 byte) => 80 E8 00 05 4F 07 00 4C 00 01 10 18 8C 00 02 7A    ....O..L.......z     05 30 8F 00 08 3D 8C 00 00 18 1D 04 41 18 1D 25    .0...=......A..%     8B 00 01 7A 02 21 18 8B 00 06 60 03 7A 19 8B 00    ...z.!....`.z...     04 2D 1A 04 25 73 00 09 00 00 00 00 00 0F 11 6D    .-..%s.........m     00 8D 00 07 7A 02 10 1C 04 6B 07 7C 00 03 70 05    ....z....k.|..p.     7C 00 05 78 00                                     |..x. (1101 usec) <= 00 90 00                                           ... Status: No Error Method.cap loaded (79 byte) Start loading StaticField.cap (15 byte) => 80 E8 00 06 0F 08 00 0C 00 02 00 00 00 00 00 00    ................     00 02 90 90 00                                     ..... (1069 usec) <= 00 90 00                                           ... Status: No Error StaticField.cap loaded (15 byte) Start loading ConstantPool.cap (41 byte) => 80 E8 00 07 29 05 00 26 00 09 06 00 00 01 03 80    ....)..&........     03 02 06 80 03 00 05 00 00 00 03 80 0A 01 05 00    ................     00 01 03 80 03 03 06 80 07 01 01 00 02 00 00       ............... (3134 usec) <= 00 90 00                                           ... Status: No Error ConstantPool.cap loaded (41 byte) Start loading RefLocation.cap (16 byte) => 80 E8 00 08 10 09 00 0D 00 00 00 09 05 06 04 0A    ................     07 07 13 0A 05 00                                  ...... (2067 usec) <= 00 90 00                                           ... Status: No Error RefLocation.cap loaded (16 byte) Start loading Descriptor.cap (114 byte) => 80 E8 00 09 72 0B 00 6F 01 00 01 00 02 00 00 02    ....r..o........     00 04 00 09 00 00 00 80 03 01 09 00 00 01 80 03    ................     00 81 00 01 00 14 00 05 00 00 00 00 01 09 00 08    ................     00 16 00 12 00 00 00 00 07 01 00 1C 00 19 00 1F    ................     00 00 00 00 02 09 00 3D 00 1D 00 0D 00 00 00 00    .......=........     00 09 00 14 00 16 00 14 00 1F 00 21 00 1F 00 23    ...........!...#     00 25 FF FF 01 10 04 B4 31 06 68 00 A1 02 33 01    .%......1.h...3.     30 01 B0 01 20 02 41 00                            0... .A. (1344 usec) <= 00 90 00                                           ... Status: No Error Descriptor.cap loaded (114 byte) Start loading Debug.cap (608 byte) => 80 E8 00 0A FA 0C 02 5D 00 1A 00 29 54 65 73 74    .......]...)Test     5F 63 6F 6D 6D 6F 6E 5F 66 75 6E 63 74 69 6F 6E    _common_function     2F 54 65 73 74 5F 63 6F 6D 6D 6F 6E 5F 66 75 6E    /Test_common_fun     63 74 69 6F 6E 00 19 6A 61 76 61 63 61 72 64 2F    ction..javacard/     66 72 61 6D 65 77 6F 72 6B 2F 41 70 70 6C 65 74    framework/Applet     00 19 54 65 73 74 5F 63 6F 6D 6D 6F 6E 5F 66 75    ..Test_common_fu     6E 63 74 69 6F 6E 2E 6A 61 76 61 00 0A 62 50 31    nction.java..bP1     5F 43 6C 61 73 73 31 00 01 42 00 0A 62 50 31 5F    _Class1..B..bP1_     43 6C 61 73 73 32 00 06 3C 69 6E 69 74 3E 00 03    Class2..<init>..     28 29 56 00 04 74 68 69 73 00 2B 4C 54 65 73 74    ()V..this.+LTest     5F 63 6F 6D 6D 6F 6E 5F 66 75 6E 63 74 69 6F 6E    _common_function     2F 54 65 73 74 5F 63 6F 6D 6D 6F 6E 5F 66 75 6E    /Test_common_fun     63 74 69 6F 6E 3B 00 07 69 6E 73 74 61 6C 6C 00    ction;..install.     07 28 5B 42 53 42 29 56 00 06 62 41 72 72 61 79    .([BSB)V..bArray     00 02 5B 42 00 07 62 4F 66 66 73 65 74 00 01 53    ..[B..bOffset..S     00 07 62 4C 65 6E 67 74 68 00 07 70 72 6F 63 00    ..bLength..proc. (937268 nsec) <= 00 90 00                                           ... Status: No Error => 80 E8 00 0B FA 65 73 73 00 1C 28 4C 6A 61 76 61    .....ess..(Ljava     63 61 72 64 2F 66 72 61 6D 65 77 6F 72 6B 2F 41    card/framework/A     50 44 55 3B 29 56 00 04 61 70 64 75 00 19 4C 6A    PDU;)V..apdu..Lj     61 76 61 63 61 72 64 2F 66 72 61 6D 65 77 6F 72    avacard/framewor     6B 2F 41 50 44 55 3B 00 03 62 75 66 00 05 47 65    k/APDU;..buf..Ge     74 50 31 00 04 28 42 29 42 00 07 69 64 63 6C 61    tP1..(B)B..idcla     73 73 00 14 54 65 73 74 5F 63 6F 6D 6D 6F 6E 5F    ss..Test_common_     66 75 6E 63 74 69 6F 6E 00 19 00 01 00 00 00 01    function........     00 02 00 01 00 02 00 00 02 00 04 00 03 00 04 00    ................     09 00 00 00 00 00 05 00 04 00 09 00 00 00 01 00    ................     06 00 07 00 01 00 01 02 00 05 00 01 00 01 00 00    ................     08 00 09 00 00 00 05 00 00 00 04 00 0F 00 0A 00    ................     0B 00 09 00 08 02 00 12 00 03 00 04 00 00 0C 00    ................     0D 00 00 00 12 01 00 0E 00 0F 00 00 00 12 02 00    ................     10 00 04 00 00 00 12 00 00 00 07 00 14 00 08 00    ................     0D 00 15 00 0E 00 10 00 14 00 11 00 11 00 16 00    ................ (785421 nsec) <= 00 90 00                                           ... Status: No Error => 80 E8 80 0C 6C 00 11 00 12 00 01 00 1C 02 00 1F    ....l...........     00 03 00 06 00 00 08 00 09 00 00 00 1F 01 00 13    ................     00 14 00 00 00 1F 02 00 15 00 0D 00 0C 00 13 00    ................     00 00 05 00 1B 00 06 00 06 00 1D 00 07 00 0B 00    ................     20 00 0C 00 17 00 21 00 18 00 1D 00 27 00 1E 00     .....!.....'...     1E 00 29 00 16 00 17 00 09 00 3D 02 00 0D 00 01    ..).......=.....     00 01 00 00 18 00 04 00 00 00 0D 00 00 00 0C 00    ................     2F 00                                              /. (1986 usec) <= 00 90 00                                           ... Status: No Error Debug.cap loaded (608 byte) Load report:   999 bytes loaded in 0.0 seconds   effective code size on card:       + package AID       6       + applet AIDs       15       + classes           17       + methods           79       + statics           2       + exports           0      ------------------------------         overall           119  bytes cm>  install -i 0102030405010101  -q C9#() 010203040501 0102030405010101 => 80 E6 0C 00 1F 06 01 02 03 04 05 01 08 01 02 03    ................     04 05 01 01 01 08 01 02 03 04 05 01 01 01 01 00    ................     02 C9 00 00 00                                     ..... (2346 usec) <= 00 90 00                                           ... Status: No Error cm>  upload -c -d -b 250 "D:\WorkSpace\Test_Common_Funcation\bin\Test_Class_2\javacard\Test_Class_2.cap" jcshell: Cannot read <D:\WorkSpace\Test_Common_Funcation\bin\Test_Class_2\javacard\Test_Class_2.cap>: java.io.FileNotFoundException: D:\WorkSpace\Test_Common_Funcation\bin\Test_Class_2\javacard\Test_Class_2.cap (The system cannot find the path specified) cm>  install -i 0102030405010303  -q C9#() 01020304050103 0102030405010303 => 80 E6 0C 00 20 07 01 02 03 04 05 01 03 08 01 02    .... ...........     03 04 05 01 03 03 08 01 02 03 04 05 01 03 03 01    ................     00 02 C9 00 00 00                                  ...... (1231 usec) <= 6A 88                                              j. Status: Reference data not found jcshell: Error code: 6a88 (Reference data not found) jcshell: Wrong response APDU: 6A88 Unexpected error; aborting execution
                Thank very much!

                Edited by: WhoKnows on Apr 20, 2012 9:32 PM
                • 5. Re: How can we create a common function for 2 or more java card applets?
                  921719
                  Hello all,
                  Thanks to help me!
                  • 6. Re: How can we create a common function for 2 or more java card applets?
                    921719
                    Please help me! Here is my urgent task
                    • 7. Re: How can we create a common function for 2 or more java card applets?
                      893199
                      Try putting them in the same package. I think you're running into applet firewall issues.
                      • 8. Re: How can we create a common function for 2 or more java card applets?
                        Adriaan
                        Similar discussion here: Accessing a Static Array in one CAP file from another CAP in same project

                        Previous poster is correct, this is a firewall feature and some access is possible if you put your classes in the same package.

                        More info on the firewall: http://java.sun.com/developer/Books/consumerproducts/javacard/ch09.pdf

                        Also have a look at the Shareable Interface Object (SIO) framework.


                        Adriaan
                        • 9. Re: How can we create a common function for 2 or more java card applets?
                          safarmer
                          Hi,

                          Make the common code a part of a library package. A library package is one that does not contain any applet classes. Then you can call the methods from any other class. One issue is that any objects (arrays etc) created by one applet will not be accessible by an applet in a different package. If they are just helper methods you might not have a problem. If things get more complicated you will have to use sharable objects.

                          Shane