如何使用Commandline生成Java CAP文件

时间:2016-02-18 10:03:47

标签: java javacard

我正在尝试为符合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);
        }
}

因此,我将其保存在名为.java的{​​{1}}文件中,然后将其编译为HelloWorldApplet文件,如下所示:

.class

Q1:这是什么警告?

之后,我尝试将此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 文件转换为.class格式:

.cap

好吧,如您所见,我收到错误:未找到输入类目录E:\ ToCompile \ helloWorldPackage。

Q2:转换器为什么要寻找此路径?我将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 目录指定为类目录,但它将我指定的路径与我的程序包名称连接起来!为什么呢?

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

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

我将.cap及其生成的类文件(即HelloWorldApplet.java)移动到同一目录中名为 helloWorldPackage (My Applet包名称)的文件夹中。然后重试转换命令:

HelloWorldApplet.class

由于该错误,我在命令中添加了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。 (我的第三季答案):

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.

1 个答案:

答案 0 :(得分:2)

  

警告:[options] bootstrap类路径未与-source 1.2一起设置

     

这是什么警告?

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

  

Q2:转换器为什么要寻找此路径?我将E:\ ToCompile目录指定为类目录,但它将我指定的路径与我的程序包名称连接起来!为什么呢?

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

  

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

如果你没有向转换器提供Applet类/ AID,那么该包被认为是一个库包(它是一个没有自己状态的包,只包含可供其他库使用的代码当然还有Applets)。