6

I'm trying to generate CAP file of the following program for a Java Card 2.2.1 platform compliant smart card:

package helloWorldPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class HelloWorldApplet extends Applet 
{
    private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};
    private static final byte HW_CLA = (byte)0x80;
    private static final byte HW_INS = (byte)0x00;

    public static void install(byte[] bArray, short bOffset, byte bLength) 
        {
        new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        }

    public void process(APDU apdu) 
        {
        if (selectingApplet()) 
            {
            return;
            }

        byte[] buffer = apdu.getBuffer();
        byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
        byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

        if (CLA != HW_CLA)
            {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
            }

        switch ( INS ) 
            {
            case HW_INS:
                getHelloWorld( apdu );
                break;
            default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        }

    private void getHelloWorld( APDU apdu)
        {
        byte[] buffer = apdu.getBuffer();
        short length = (short) helloWorld.length;
        Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);
        apdu.setOutgoingAndSend((short)0, length);
        }
}

So, I saved it in a .java file named HelloWorldApplet and then compile it to .class file as below:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>javac -g -source 1.2 -target 1.2 -cp "E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_
1-win-dom\lib\api.jar" "E:\ToCompile\HelloWorldApplet.java"
warning: [options] bootstrap class path not set in conjunction with -source 1.2
1 warning

Q1: What is this warning for?

After that, I tried to convert this .class file to its .cap form:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -classdir E:\ToCompile helloWorldPackage 0xa0:0x0:0x0:0x0:0x6
2:0x3:0x1:0xc:0x6:0x1 1.0
error: input class directory E:\ToCompile\helloWorldPackage not found.
Usage:  converter  <options>  package_name  package_aid  major_version.minor_version
OR
converter -config <filename>
                    use file for all options and parameters to converter
Where options include:
        -classdir <the root directory of the class hierarchy>
                      set the root directory where the Converter
                      will look for classes
        -i            support the 32-bit integer type
        -exportpath  <list of directories>
                      list the root directories where the Converter
                      will look for export files
        -exportmap    use the token mapping from the pre-defined export
                      file of the package being converted. The converter
                      will look for the export file in the exportpath
        -applet <AID class_name>
                      set the applet AID and the class that defines the
                      install method for the applet
        -d <the root directory for output>
        -out  [CAP] [EXP] [JCA]
                      tell the Converter to output the CAP file,
                      and/or the JCA file, and/or the export file
        -V, -version  print the Converter version string
        -v, -verbose  enable verbose output
        -help         print out this message
        -nowarn       instruct the Converter to not report warning messages
        -mask         indicate this package is for mask, so restrictions on
                      native methods are relaxed
        -debug        enable generation of debugging information
        -nobanner     suppress all standard output messages
        -noverify     turn off verification. Verification is default

Well, as you see I receive error: input class directory E:\ToCompile\helloWorldPackage not found..

Q2: Why the converter looking for this path? I specified E:\ToCompile directory as class directory, but it concatenated my specified path with my program package name! why?

Q3: When we open a .cap file using Winrar, we can find our Package AID and our Applet AID in header.cap and applet.cap files in it. In the above steps, I only specify my package AID, So how it assign the Applet AID in the cap file?


Update:(Thanx to Mr Bodewes Answer)

I moved HelloWorldApplet.java and its generated class file (i.e HelloWorldApplet.class) to a folder named helloWorldPackage (My Applet package name), in the same directory. And then retried convert command:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -classdir E:\ToCompile\ helloWorldPackage 0xa0:0x0:0x0:0x0:0x
62:0x3:0x1:0xc:0x6:0x1 1.0

Java Card 2.2.2 Class File Converter, Version 1.3
Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

parsing E:\ToCompile\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
error: export file framework.exp of package javacard.framework not found.

conversion completed with 1 errors and 0 warnings.

Due to the error, I added -exportpath parameter to the command and tried again:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -exportpath E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_
1-win-dom\api_export_files -classdir E:\ToCompile\ helloWorldPackage 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1 1.0

Java Card 2.2.2 Class File Converter, Version 1.3
Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

parsing E:\ToCompile\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
parsing E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\api_export_files\javacard\framework\javacard\framework.exp
parsing E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\api_export_files\java\lang\javacard\lang.exp
writing E:\ToCompile\helloWorldPackage\javacard\helloWorldPackage.exp
writing E:\ToCompile\helloWorldPackage\javacard\helloWorldPackage.jca
error:  Static array initialization in class helloWorldPackage/HelloWorldApplet in library package not allowed.

Cap file generation failed.

conversion completed with 1 errors and 0 warnings.

After a little struggles, I finally found that, there is a parameters for converter named applet that if I don't use it (in the form of -applet <AppletAID> AppletClassName) in the command line, then the converter consider that package as a library package (That doesn't have Applet AID in the contents), but if I add this parameter to the command line as below, the converter consider the package as Applet package and use the AID I added to parameters for assigning the AID in the header.cap (or maybe applet.cap) inside the cap file. (Answer of my Q3):

C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -exportpath C:\Users\AmirEbrahim\Desktop\JC_C
onverter\JCDK\java_card_kit-2_2_1-win-dom\bin  -classdir C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin -applet 0xa0:0
x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1:0x2 HelloWorldApplet helloWorldPackage 0xa0:0x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1 1.0

Java Card 2.2.2 Class File Converter, Version 1.3
Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.

parsing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
parsing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\javacard\framework\javacard\framework.exp
parsing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\java\lang\javacard\lang.exp
writing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\javacard\helloWorldPackage.exp
writing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\javacard\helloWorldPackage.jca

conversion completed with 0 errors and 0 warnings.

1 Answers1

3

warning: [options] bootstrap class path not set in conjunction with -source 1.2

What is this warning for?

The warning is because you are compiling for a 1.2 source code compatibility with a newer boot classpath, i.e. the one of your current JRE. Now if you'd use newer classes in the JRE then you would not be Java 1.2 compatible. This is of course not an issue if all your classes use just Java Card classes like you should. In other words, you can safely ignore it.

Q2: Why the converter looking for this path? I specified E:\ToCompile directory as class directory, but it concatenated my specified path with my program package name! why?

In principle the source code folder structure of Java applications isn't defined. In practice however the sources are placed in folders that reflect package names. So in general if your package statement reads package package1.package2; for MyApplet then most tools expect the source code to be in package1/package2/MyApplet.java within your source folder. Class files are put in folders the same way. This is true for any Java application, not just Java Card.

Q3: When we open a .cap file using Winrar, we can find our Package AID and our Applet AID in header.cap and applet.cap files in it. In the above steps, I only specify my package AID, So how it assign the Applet AID in the cap file?

If you don't supply an Applet class / AID to the converter then the package is considered a library package (which is a package with no state of its own, consisting just of code that can be used by other libraries and of course Applets).

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263