As you may know, and as it is mentioned in Java Card Development Kit User Guide, the key to writing large applications for the Java Card platform is to divide the code into individual package units. The most important limitation on a package is the 64KB limitation on the maximum component size. This is especially true for the Method component: if the size of an application’s Method component exceeds 64KB, then the Java Card converter will not process the package and will return an error.
So, we need to use Java Card library packages in some special cases. My question is how to create and how to use these Java Card library packages?
What am I did so far?
Well, I wrote a really simple Java Class containing a single method named sayHello()
as below:
package libPack;
import javacard.framework.*;
public class mylib {
public static final byte[] hello = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
protected mylib() {
}
public void sayHello(APDU apdu) {
byte[] buffer = apdu.getBuffer();
Util.arrayCopyNonAtomic(hello, (short)0x00, buffer, (short)0x00, (short)hello.length);
apdu.setOutgoingAndSend((short)0x00, (short)buffer.length);
}
}
As I couldn't find any option in my IDE-s (Netbeans & Eclipse) to create cap
file of library packages (Those have plugins for creating cap
from Applet packages only, I guess), I used command line to create my library's cap file as below:
1. Generating the .class
file of above .java
program:
CMD:> javac -g -source 1.2 -target 1.2 -cp "D:\JCDK\java_card_kit-2_2_2\lib\api.jar" "D:\LibSrc\mylib.java"
warning: [options] bootstrap class path not set in conjunction with -source 1.2
1 warning
CMD:>
Above command generate a .class
file that its name is my library class name. I create a folder in the same directory and named it "libPack" (the program's package name) and then I moved this .class
file into it.
2. Converting the created .class
file to .cap
file:
CMD:> D:\JCDK\java_card_kit-2_2_2\bin\converter.bat -debug -verbose -exportpath D:\JCDK\java_card_kit-2_2_2\api_export_files -classdir D:\LibSRC libPack 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 D:\LibSRC\libPack\mylib.class
converting libPack.mylib
parsing D:\JCDK\java_card_kit-2_2_2\api_export_files\java\lang\javacard\lang.exp
parsing D:\JCDK\java_card_kit-2_2_2\api_export_files\javacard\framework\javacard\framework.exp
writing D:\LibSRC\libPack\javacard\libPack.exp
writing D:\LibSRC\libPack\javacard\libPack.jca
conversion completed with 0 errors and 0 warnings.
CMD:>
Above command, generate libPack.cap and libPack.exp files in "libPack\javacard" directory.
Questions:
- Am I did the Java Card Library Package generation process in a right way?
- How can I use this library package in my applets to reduce my applet packages smaller in size?
Update: (Based on dear Vojta's answer)
To make whole the process IDE-Independent, I create a .jar
file of my library .class
file using the following command in the commandline again:
CMD:> jar cfv mylib.jar D:\LibSRC\libPack\mylib.class
CMD:>
The above command, created "mylib.jar" from the "mylib.class" file in the command line current directory.
After that I added this "mylib.jar" file and its already created .exp
file (previous step using Converter) to my Applet project in Netbeans IDE, in the same way that I explained here.
Now I want to use the sayHello()
method of my library in my applet:
As you see above, I still receive an error. What's that? As far as I know, I can't define static methods in the library packages (right?), so where I can use library methods?