如何使用命令行生成 java 个 CAP 文件

How to generate java CAP files using Commandline

我正在尝试为 Java Card 2.2.1 平台兼容的智能卡生成以下程序的 CAP 文件:

package helloWorldPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class HelloWorldApplet extends Applet 
{
    private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};
    private static final byte HW_CLA = (byte)0x80;
    private static final byte HW_INS = (byte)0x00;

    public static void install(byte[] bArray, short bOffset, byte bLength) 
        {
        new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        }

    public void process(APDU apdu) 
        {
        if (selectingApplet()) 
            {
            return;
            }

        byte[] buffer = apdu.getBuffer();
        byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
        byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

        if (CLA != HW_CLA)
            {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
            }

        switch ( INS ) 
            {
            case HW_INS:
                getHelloWorld( apdu );
                break;
            default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
            }
        }

    private void getHelloWorld( APDU apdu)
        {
        byte[] buffer = apdu.getBuffer();
        short length = (short) helloWorld.length;
        Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);
        apdu.setOutgoingAndSend((short)0, length);
        }
}

所以,我将其保存在名为 HelloWorldApplet.java 文件中,然后将其编译为 .class 文件,如下所示:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>javac -g -source 1.2 -target 1.2 -cp "E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_
1-win-dom\lib\api.jar" "E:\ToCompile\HelloWorldApplet.java"
warning: [options] bootstrap class path not set in conjunction with -source 1.2
1 warning

问题 1:此警告的用途是什么?

之后,我尝试将此 .class 文件转换为其 .cap 形式:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -classdir E:\ToCompile helloWorldPackage 0xa0:0x0:0x0:0x0:0x6
2:0x3:0x1:0xc:0x6:0x1 1.0
error: input class directory E:\ToCompile\helloWorldPackage not found.
Usage:  converter  <options>  package_name  package_aid  major_version.minor_version
OR
converter -config <filename>
                    use file for all options and parameters to converter
Where options include:
        -classdir <the root directory of the class hierarchy>
                      set the root directory where the Converter
                      will look for classes
        -i            support the 32-bit integer type
        -exportpath  <list of directories>
                      list the root directories where the Converter
                      will look for export files
        -exportmap    use the token mapping from the pre-defined export
                      file of the package being converted. The converter
                      will look for the export file in the exportpath
        -applet <AID class_name>
                      set the applet AID and the class that defines the
                      install method for the applet
        -d <the root directory for output>
        -out  [CAP] [EXP] [JCA]
                      tell the Converter to output the CAP file,
                      and/or the JCA file, and/or the export file
        -V, -version  print the Converter version string
        -v, -verbose  enable verbose output
        -help         print out this message
        -nowarn       instruct the Converter to not report warning messages
        -mask         indicate this package is for mask, so restrictions on
                      native methods are relaxed
        -debug        enable generation of debugging information
        -nobanner     suppress all standard output messages
        -noverify     turn off verification. Verification is default

嗯,如你所见,我收到 错误:输入 class 目录 E:\ToCompile\helloWorldPackage 未找到。

Q2:转换器为什么要找这个路径?我把E:\ToCompile目录指定为class目录,但是它把我指定的路径和我的程序包名拼接起来了!为什么?

Q3:当我们用Winrar打开一个.cap文件时,我们可以在其中的header.cap和applet.cap文件中找到我们的Package AID和Applet AID。在上面的步骤中,我只指定了我的包AID,那么它如何在cap文件中分配Applet AID?


更新:(感谢 Bodewes 先生的回答)

我将 HelloWorldApplet.java 及其生成的 class 文件(即 HelloWorldApplet.class)移动到名为 helloWorldPackage 的文件夹(我的小程序包名称) , 在同一目录中。然后重试转换命令:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -classdir E:\ToCompile\ helloWorldPackage 0xa0:0x0:0x0:0x0:0x
62: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 E:\ToCompile\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
error: export file framework.exp of package javacard.framework not found.

conversion completed with 1 errors and 0 warnings.

由于错误,我在命令中添加了-exportpath参数,然后再次尝试:

E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -exportpath E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_
1-win-dom\api_export_files -classdir E:\ToCompile\ helloWorldPackage 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 E:\ToCompile\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
parsing E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\api_export_files\javacard\framework\javacard\framework.exp
parsing E:\ToCompile\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\api_export_files\java\lang\javacard\lang.exp
writing E:\ToCompile\helloWorldPackage\javacard\helloWorldPackage.exp
writing E:\ToCompile\helloWorldPackage\javacard\helloWorldPackage.jca
error:  Static array initialization in class helloWorldPackage/HelloWorldApplet in library package not allowed.

Cap file generation failed.

conversion completed with 1 errors and 0 warnings.

经过一番折腾,终于发现,converter有个参数叫applet,如果我不用的话(-applet <AppletAID> AppletClassName的形式)在命令行中,转换器将该包视为库包(内容中没有 Applet AID),但如果我将此参数添加到命令行,如下所示,转换器将该包视为 Applet 包,并且使用我添加到参数中的 AID 来分配 cap 文件中 header.cap(或者可能是 applet.cap)中的 AID。 (我Q3的答案):

C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin>converter -debug -verbose -exportpath C:\Users\AmirEbrahim\Desktop\JC_C
onverter\JCDK\java_card_kit-2_2_1-win-dom\bin  -classdir C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin -applet 0xa0:0
x0:0x0:0x0:0x62:0x3:0x1:0xc:0x6:0x1:0x2 HelloWorldApplet helloWorldPackage 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 C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\HelloWorldApplet.class
converting helloWorldPackage.HelloWorldApplet
parsing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\javacard\framework\javacard\framework.exp
parsing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\java\lang\javacard\lang.exp
writing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\javacard\helloWorldPackage.exp
writing C:\Users\AmirEbrahim\Desktop\JC_Converter\JCDK\java_card_kit-2_2_1-win-dom\bin\helloWorldPackage\javacard\helloWorldPackage.jca

conversion completed with 0 errors and 0 warnings.

warning: [options] bootstrap class path not set in conjunction with -source 1.2

What is this warning for?

警告是因为您正在编译 1.2 源代码与较新的引导 class 路径的兼容性,即您当前的 JRE 之一。现在,如果您要在 JRE 中使用较新的 classes,那么您将不会与 Java 1.2 兼容。如果您所有的 classes 都像您应该的那样只使用 Java Card classes,那么这当然不是问题。换句话说,您可以放心地忽略它。

Q2: Why the converter looking for this path? I specified E:\ToCompile directory as class directory, but it concatenated my specified path with my program package name! why?

原则上 Java 应用程序的源代码文件夹结构未定义。然而在实践中,源被放置在反映包名称的文件夹中。因此,一般来说,如果您的包语句读取 package package1.package2; for MyApplet,那么大多数工具都希望源代码位于源文件夹中的 package1/package2/MyApplet.java 中。 Class 文件以同样的方式放在文件夹中。对于任何 Java 应用程序都是如此,而不仅仅是 Java 卡。

Q3: When we open a .cap file using Winrar, we can find our Package AID and our Applet AID in header.cap and applet.cap files in it. In the above steps, I only specify my package AID, So how it assign the Applet AID in the cap file?

如果您不向转换器提供 Applet class / AID,那么该包将被视为库包(这是一个没有自己状态的包,仅包含可以被其他库使用,当然还有 Applets)。