How to create and use Java Card library packages?

ε祈祈猫儿з 提交于 2019-12-04 11:22:43

Any Java Card package with some public classes and public methods can be used as a library. If you use Eclipse JCOP Tools, you can build your library very easily: the cap file is created automatically in folder:

/[workspace]/[project]/bin/[path according to package]/javacard

A Java Card library is just any package; even a package with an Applet can be used as one. So there is no real difference between building a "common" cap file and a "library" cap file.


Note there is a little difference between objects implementing Shareable interface and static methods in your library package. Shareable interface can be used to access object instances in context A through the firewall from the context B. However, static methods can be accesses from any context - no firewall rules apply. There is a nice overview of AppletIsolation and Object Sharing here. The most important paragraph on static methods is 6.1.6:

Instances of classes—objects—are owned by contexts; classes themselves are not. There is no runtime context check that can be performed when a class static field is accessed. Neither is there a context switch when a static method is invoked.

Public static fields and public static methods are accessible from any context: static methods execute in the same context as their caller.

Objects referenced in static fields are just regular objects. They are owned by whomever created them and standard firewall access rules apply. If it is necessary to share them across multiple contexts, then these objects need to be Shareable Interface Objects (SIOs).

Of course, the conventional Java technology protections are still enforced for static fields and methods. In addition, when applets are installed, the Installer verifies that each attempt to link to an external static field or method is permitted. Installation and specifics about linkage are beyond the scope of this specification.

Shortly speaking: no static objects in your static library = no Shareable needed.


You sometimes need to use an existing library in your new applet, although you do not have the source code of the library. The library might have been loaded to your card by the vendor or by some third party. You need a jar file and an exp file of the library to be able to use it in your applet.

You need class files of the library in a common Java jar file to build your new class files by the Java compiler. Then you need some extra information for the Java Card Converter to link your code with library classes and their methods. That is what exp files are used for. The exp file describes the interface and dependencies of all public components in a cap file. Eclipse JCOP Tools creates the exp file together with the cap file in the same folder, as well as the Java Card Converter does. (See the documentation by Oracle)

The exp file and the jar file is all you need to build your code which uses the library. Just put them both into your project and make sure the jar file is on the build path of your project.

Feel free to ask.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!