7 Programming Large Java Card Applications with Multiple Packages
Since Java Card Version 3.1, multiple Java packages can be bundled into one Java Card CAP file using the extended CAP file format.
This feature enables you to:
- Keep a modular design by having applications or libraries made of multiple packages.
- Distribute an application with the libraries it relies on.
- Control the visibility of each of the packages deployed in a CAP file.
- Overcome the size limitation of 64 K in compact CAP files.
The converter uses the extended CAP file format when multiple packages are used in a single CAP file or when the converted code for the whole CAP file exceeds the size of 64 KB.
Java Card products only optionally support the extended CAP file format. Before using this extended CAP file format, it is important to check if the target Java Card product supports this feature or not.
Refer to Java Card Platform Virtual Machine Specification, Classic Edition, Version 3.2, for more information on the CAP file and its format.
Topics:
- CAP File Identification
- Package Visibility
- Firewall Context
- Extended CAP Accessibility Example
- Design Rules for a Java Card Application with Large Method Component
CAP File Identification
Each CAP file has an AID and a version. The AID and version values are dependent on the format of the CAP file (compact and extended) as follows:
-
When the CAP file contains a unique package and it is in the compact format, its AID and version are same as the AID and version of the package it contains.
-
When the CAP file contains multiple packages and it is in the extended format, it has its own AID and version, independent of the AIDs and versions of the packages it contains.
Package Visibility
The extended CAP file format offers more flexibility for the package visibility as follows:
-
Each public package inside a CAP file has an AID and version. This AID and version uniquely identify this package when a package in another CAP file is importing it.
-
Private packages inside a CAP file or packages that have no exported information (like an applet package with no
Shareableinterfaces) have no AID and version. In addition, because nothing is exported, the Converter does not generate an export file. Packages in other CAP files imports nothing from such packages. -
Unlike in an applet compact CAP file, if an extended CAP file is an applet CAP file, then the public library packages that are contained in the CAP file are exported as if they were present in a compact CAP file. In addition, the public applet packages contained in an extended CAP file are exported individually based on the same rules as for the compact CAP files (only public
Shareableinterfaces). -
Packages inside a bundle are visible to each other and the standard Java access rules (
public,protected,package, orprivate) apply, irrespective of whether the packages arepublicorprivate.
Firewall Context
If a CAP file contains at least one package with one or more non-abstract classes that extend the javacard.framework.Applet class, then it is associated with a firewall context.
Refer to the Java Card Platform Runtime Environment Specification, Classic Edition, Version 3.2, for more details.
Extended CAP Accessibility Example
To understand the extended CAP file accessibility, let’s consider a scenario as shown in the following figure:
Figure 7-1 Extended CAP Accessibility Example

Description of the illustration extended-cap-applets.png
The following table describes the package level access under different access conditions (1, 2, and 3):
Table 7-1 Package Level Access
| Accessibility | package1 | package2 | package3 | package4 | package5 |
|---|---|---|---|---|---|
|
|
N/A |
Yes (1) and (2) |
Yes (1) |
Yes (1) |
Yes (1), (2), and (3) |
|
|
Yes (1) and (2) |
N/A |
Yes (1) |
Yes (1) |
Yes (1), (2), and (3) |
|
|
Yes (1) and (2) |
Yes (1) and (2) |
N/A |
Yes (1) |
Yes (1), (2), and (3) |
|
|
Yes (1) and (2) |
Yes (1) and (2) |
Yes (1) |
N/A |
Yes (1), (2), and (3) |
|
|
Yes (1) and (2) |
No (1) |
No (1) and (3) |
Yes (1) and (3) |
N/A |
The following are the access conditions (1, 2, and 3) that are listed in the table:
- Exported packages in an extended CAP file - Packages in an extended CAP file can be marked as
publicorprivate. Only thepublicpackages are accessible from packages in another CAP file. However, all packages are accessible within the same CAP file. - Java access rules - Code access conforms to Java accessibility rules (
private,public,package,protected, and so on). For example, inside theClass1orClass2methods, aClass3()or aClass4()constructor can be called (only if the constructors arepublic) or other public methods fromClass3andClass4, even ifClass3andClass4are Java Card applets. - Java Card access rules for package containing an Applet - A public package containing a class extending the
javacard.framework.Appletclass does not export all itspublicclasses and interfaces. Only the interfaces extending thejavacard.framework.Shareableinterface or the classes implementing it are exported and visible from other packages. For example, code frompackage5can access only theClass5inpackage4and cannot access content ofpackage3because nothing is exported.
Design Rules for a Java Card Application with Large Method Component
In compact CAP files, the Method Component is limited to a 64 KB size. This can be a constraint if an application has many features, if a library has a large API, or if it is too large to fit into that size after conversion.
The extended CAP file offers a solution to this by creating a Method Component that has a maximum size of eight megabytes. For large applications, the extended mode is preferable.
The Converter splits the large Method Component into blocks, each with a maximum size of 64 KB. It is important to note that methods cannot be divided between two blocks and all exception handlers for a method must be contained in the same block of the method code. Because of this, when programming large Java Card applications for extended CAPs, the method code size must not be too large and specialization pattern must be used whenever possible.