java - 如何创建和使用 Java Card 库包?

标签 java javacard

您可能知道,正如 Java Card 开发工具包用户指南 中提到的那样,为 Java Card 平台编写大型应用程序的关键是将代码分成单独的包单元。包最重要的限制是最大组件大小的 64KB 限制。这对于 方法组件:如果应用程序的方法组件大小超过 64KB, 那么 Java Card 转换器将不会处理该包并将返回一个错误。

所以,在某些特殊情况下,我们需要使用Java Card库包。我的问题是如何创建和使用这些 Java Card 库包?

到目前为止我做了什么?

好吧,我写了一个非常简单的 Java 类,其中包含一个名为 sayHello() 的方法,如下所示:

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);
    }
}

因为我在我的 IDE-s(Netbeans 和 Eclipse)中找不到任何选项来创建库包的 cap 文件(那些有用于创建 cap 的插件我猜只有 Applet 包),我使用命令行创建我的库的 cap 文件,如下所示:

1.生成上述.java程序的.class文件:

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:>

以上命令生成一个.class 文件,其名称是我的库类名。我在同一目录中创建了一个文件夹并将其命名为“libPack”(程序的包名称),然后我将这个 .class 文件移入其中。

2.将创建的.class文件转换为.cap文件:

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:>

以上命令,在“libPack\javacard”目录下生成libPack.caplibPack.exp文件。

问题:

  1. 我是否以正确的方式执行了 Java Card 库包生成过程?
  2. 如何在我的小程序中使用这个库包来减小我的小程序包的大小?

更新:(基于亲爱的 Vojta 的回答)

为了使整个过程与 IDE 无关,我再次在命令行中使用以下命令为我的库 .class 文件创建了一个 .jar 文件:

CMD:> jar cfv mylib.jar D:\LibSRC\libPack\mylib.class
CMD:>

上述命令,从命令行当前目录中的“mylib.class”文件创建了“mylib.jar”。

之后,我将这个“mylib.jar”文件及其已经创建的 .exp 文件(上一步使用 Converter)添加到我在 Netbeans IDE 中的 Applet 项目中,方法与我解释 here 的方式相同.

现在我想在我的小程序中使用我的库的 sayHello() 方法:

enter image description here

如您所见,我仍然收到错误消息。那是什么?据我所知,我不能在库包中定义静态方法(对吗?),那么我可以在哪里使用库方法?

最佳答案

任何具有一些公共(public)类和公共(public)方法的 Java Card 包都可以用作库。如果您使用 Eclipse JCOP 工具,您可以非常轻松地构建您的库:cap 文件会在文件夹中自动创建:

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

Java Card 库就是任何包;即使是带有 Applet 的包也可以作为一个包使用。因此,构建“通用”cap 文件和“库”cap 文件之间没有真正的区别。


请注意,实现 Shareable 接口(interface)的对象与库包中的静态方法之间存在细微差别。 Shareable 接口(interface)可用于通过防火墙从上下文 B 访问上下文 A 中的对象实例。但是,static 方法可以从任何上下文访问 - 不适用防火墙规则。 AppletIsolation and Object Sharing here 有一个很好的概述.关于static 方法最重要的段落是 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.

简而言之:静态库中没有静态对象 = 不需要 Shareable


有时您需要在您的新小程序中使用现有的库,尽管您没有该库的源代码。该库可能已由供应商或某些第三方加载到您的卡中。您需要库的 jar 文件和 exp 文件才能在您的小程序中使用它。

您需要将库的类文件放在通用 Java jar 文件中,以便 Java 编译器构建您的新类文件。然后您需要 Java Card Converter 的一些额外信息以将您的代码与库类及其方法链接起来。这就是 exp 文件的用途。 exp 文件描述了cap 文件中所有公共(public)组件的接口(interface)和依赖关系。 Eclipse JCOP Tools 在同一文件夹中创建 exp 文件和 cap 文件,Java Card Converter 也是如此。 ( See the documentation by Oracle )

exp 文件和jar 文件是构建使用该库的代码所需的全部。只需将它们都放入您的项目中,并确保 jar 文件位于您项目的构建路径中。

欢迎提问。

关于java - 如何创建和使用 Java Card 库包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961930/

相关文章:

C# PCSC-sharp 发送/接收带有数据的自定义命令

javacard - 在 Android 中使用 Java 卡代替 SIM 卡并使用 SEEK

java - 重命名事物

java - 如何从接口(interface)获取枚举到实现该接口(interface)的类?

java - GridBagLayout 不排序我的元素

java - Selenium Java : Why I cannot find buttons with a specific text using xpath?

使用 AES 的 Java 卡加密返回 6F00

smartcard - OpenSC 是完全基于 PC/SC 还是也使用不同的命令?

java - 在 GWT 中,RPC 调用是同步的还是异步的

java - 为 jCardSim 添加对 bouncycaSTLe API 的支持