我正在尝试为符合 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);
}
}
Run Code Online (Sandbox Code Playgroud)
所以,我将它保存在一个.java名为的文件中HelloWorldApplet,然后将其编译.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
Run Code Online (Sandbox Code Playgroud)
Q1:这个警告是什么?
之后,我尝试将此.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
Run Code Online (Sandbox Code Playgroud)
好吧,如您所见,我收到错误:找不到输入类目录 E:\ToCompile\helloWorldPackage。.
Q2:转换器为什么要寻找这条路径?我将E:\ToCompile目录指定为类目录,但它将我指定的路径与我的程序包名称连接起来!为什么?
Q3:当我们.cap使用Winrar打开一个文件时,我们可以在其中的header.cap和applet.cap文件中找到我们的Package AID和我们的Applet AID。在上面的步骤中,我只指定了我的包AID,那么它是如何在cap文件中分配Applet AID的呢?
更新:(感谢 Bodewes 先生的回答)
我将HelloWorldApplet.java其生成的类文件(即HelloWorldApplet.class)移动到同一目录中名为helloWorldPackage(我的 Applet 包名称)的文件夹中。然后重试转换命令:
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.
Run Code Online (Sandbox Code Playgroud)
由于错误,我-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.
Run Code Online (Sandbox Code Playgroud)
经过一番折腾,终于发现,如果在命令行中不使用converternamed的参数applet(形式为-applet <AppletAID> AppletClassName内容中有 Applet AID),但如果我将这个参数添加到命令行,如下所示,转换器会将包视为 Applet 包,并使用我添加到参数中的 AID 来分配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.
Run Code Online (Sandbox Code Playgroud)
警告:[选项]引导类路径未与 -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。
Q3:当我们使用Winrar打开.cap文件时,我们可以在其中找到我们的Package AID和Applet AID
header.cap以及applet.cap其中的文件。在上面的步骤中,我只指定了我的包AID,那么它如何在cap文件中分配Applet AID呢?
如果您不向转换器提供 Applet 类/AID,则该包被视为库包(这是一个没有自己状态的包,仅包含可由其他库和当然 Applet 使用的代码) 。
| 归档时间: |
|
| 查看次数: |
1806 次 |
| 最近记录: |