简体   繁体   English

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

[英]How to create and use Java Card library packages?

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.您可能知道,正如Java Card Development Kit User Guide 中提到的,为 Java Card 平台编写大型应用程序的关键是将代码划分为单独的包单元。 The most important limitation on a package is the 64KB limitation on the maximum component size.对包最重要的限制是最大组件大小的 64KB 限制。 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.对于 Method 组件尤其如此:如果应用程序的 Method 组件的大小超过 64KB,则 Java Card 转换器将不会处理该包并返回错误。

So, we need to use Java Card library packages in some special cases.所以,我们需要在一些特殊情况下使用 Java Card 库包。 My question is how to create and how to use these Java Card library packages?我的问题是如何创建和如何使用这些 Java Card 库包?

What am I did so far?到目前为止我做了什么?

Well, I wrote a really simple Java Class containing a single method named sayHello() as below:好吧,我写了一个非常简单的 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);
    }
}

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:由于我在我的 IDE-s(Netbeans 和 Eclipse)中找不到任何选项来创建库包的cap文件(我猜那些只有从 Applet 包创建cap插件),我使用命令行来创建我的库的 cap文件如下:

1. Generating the .class file of above .java program: 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:>

Above command generate a .class file that its name is my library class name.上面的命令生成一个.class文件,它的名字是我的库类名。 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.我在同一目录中创建了一个文件夹并将其命名为“libPack”(程序的包名),然后我将这个.class文件移动到其中。

2. Converting the created .class file to .cap file: 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:>

Above command, generate libPack.cap and libPack.exp files in "libPack\\javacard" directory.以上命令,在“libPack\\javacard”目录下生成libPack.caplibPack.exp文件。

Questions:问题:

  1. Am I did the Java Card Library Package generation process in a right way?我是否以正确的方式执行了 Java Card 库包生成过程?
  2. 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)更新:(基于亲爱的 Vojta 的回答)

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:为了使整个过程独立于 IDE,我再次在命令行中使用以下命令创建了我的库.class文件的.jar文件:

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.上面的命令,从命令行当前目录中的“mylib.class”文件创建了“mylib.jar”。

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 .之后,我将这个“mylib.jar”文件及其已经创建的.exp文件(上一步使用 Converter)添加到 Netbeans IDE 中的 Applet 项目中,与我在此处解释的方式相同。

Now I want to use the sayHello() method of my library in my applet:现在我想在我的小程序中使用我的库的sayHello()方法:

在此处输入图片说明

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?据我所知,我不能在库包中定义静态方法(对吗?),那么我可以在哪里使用库方法?

Any Java Card package with some public classes and public methods can be used as a library.任何带有一些公共类和公共方法的 Java Card 包都可以用作库。 If you use Eclipse JCOP Tools, you can build your library very easily: the cap file is created automatically in folder:如果您使用 Eclipse JCOP 工具,您可以非常轻松地构建您的库:cap 文件会在文件夹中自动创建:

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

A Java Card library is just any package; Java Card 库就是任何包; even a package with an Applet can be used as one.即使是带有Applet的包也可以用作一个包。 So there is no real difference between building a "common" cap file and a "library" cap file.因此,构建“通用” cap文件和“库” cap文件之间没有真正的区别。


Note there is a little difference between objects implementing Shareable interface and static methods in your library package.请注意,在库包中实现Shareable接口和静态方法的对象之间存在一些差异。 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. Shareable接口可用于从上下文 B 通过防火墙访问上下文 A 中的对象实例。但是,可以从任何上下文访问static方法 - 没有防火墙规则适用。 There is a nice overview of AppletIsolation and Object Sharing here . AppletIsolation 和 Object Sharing 在这里有一个很好的概述。 The most important paragraph on static methods is 6.1.6:关于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).如果需要跨多个上下文共享它们,则这些对象需要是可共享接口对象 (SIO)。

Of course, the conventional Java technology protections are still enforced for static fields and methods.当然,传统的 Java 技术保护仍然适用于静态字段和方法。 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.简而言之:静态库中没有静态对象 = 不需要Shareable


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.您需要一个jar文件和一个库的exp文件才能在您的小程序中使用它。

You need class files of the library in a common Java jar file to build your new class files by the Java compiler.您需要通用 Java jar文件中的库类文件,以通过 Java 编译器构建新的类文件。 Then you need some extra information for the Java Card Converter to link your code with library classes and their methods.然后您需要一些额外的信息让 Java Card Converter 将您的代码与库类及其方法链接起来。 That is what exp files are used for.这就是exp文件的用途。 The exp file describes the interface and dependencies of all public components in a cap file. exp文件在一个cap文件中描述了所有公共组件的接口和依赖关系。 Eclipse JCOP Tools creates the exp file together with the cap file in the same folder, as well as the Java Card Converter does. Eclipse JCOP 工具和 Java Card Converter 一样在同一文件夹中创建exp文件和cap文件。 ( See the documentation by Oracle ) 请参阅 Oracle 的文档

The exp file and the jar file is all you need to build your code which uses the library.您只需要exp文件和jar文件即可构建使用该库的代码。 Just put them both into your project and make sure the jar file is on the build path of your project.只需将它们都放入您的项目中,并确保jar文件位于项目的构建路径上。

Feel free to ask.随意问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM