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:

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

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:

Package Visibility

The extended CAP file format offers more flexibility for the package visibility as follows:

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

package1 has access to:

N/A

Yes (1) and (2)

Yes (1)

Yes (1)

Yes (1), (2), and (3)

package2 has access to:

Yes (1) and (2)

N/A

Yes (1)

Yes (1)

Yes (1), (2), and (3)

package3 has access to:

Yes (1) and (2)

Yes (1) and (2)

N/A

Yes (1)

Yes (1), (2), and (3)

package4 has access to:

Yes (1) and (2)

Yes (1) and (2)

Yes (1)

N/A

Yes (1), (2), and (3)

package5 has access to:

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:

  1. Exported packages in an extended CAP file - Packages in an extended CAP file can be marked as public or private. Only the public packages are accessible from packages in another CAP file. However, all packages are accessible within the same CAP file.
  2. Java access rules - Code access conforms to Java accessibility rules (private, public, package, protected, and so on). For example, inside the Class1 or Class2 methods, a Class3() or a Class4() constructor can be called (only if the constructors are public) or other public methods from Class3 and Class4, even if Class3 and Class4 are Java Card applets.
  3. Java Card access rules for package containing an Applet - A public package containing a class extending the javacard.framework.Applet class does not export all its public classes and interfaces. Only the interfaces extending the javacard.framework.Shareable interface or the classes implementing it are exported and visible from other packages. For example, code from package5 can access only the Class5 in package4 and cannot access content of package3 because 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.