[英]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
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
好吧,如您所見,我收到錯誤:輸入類目錄E:\\ ToCompile \\ helloWorldPackage未找到。 。
Q2:轉換器為什么要尋找這條路徑? 我將E:\\ToCompile
目錄指定為類目錄,但它將我指定的路徑與我的程序包名稱連接起來! 為什么?
問題3:當我們使用Winrar打開.cap
文件時,我們可以在其中的header.cap和applet.cap文件中找到我們的Package AID和Applet AID。 在上面的步驟中,我只指定了我的包AID,那么如何在cap文件中分配Applet AID?
更新:(對Bodewes先生的答復)
我將HelloWorldApplet.java
及其生成的類文件(即HelloWorldApplet.class
)移動到同一目錄中名為helloWorldPackage (My 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.
由於錯誤,我在命令中添加了-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.
稍微掙扎之后,我終於發現,有一個名為applet
converter
的參數,如果我不在命令行中使用它(以-applet <AppletAID> AppletClassName
的形式),那么轉換器將該包視為一個庫包(內容中沒有Applet AID),但如果我將此參數添加到命令行,如下所示,轉換器將該包視為Applet包,並使用我添加到參數的AID來分配AID在header.cap
(或者applet.cap
)冠文件中。 (我的第三季答案):
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.
警告:[options] bootstrap類路徑未與-source 1.2一起設置
這個警告是什么?
警告是因為您正在編譯1.2源代碼與較新的引導類路徑兼容,即您當前的JRE之一。 現在,如果您在JRE中使用較新的類,那么您將不兼容Java 1.2。 如果您的所有類都只使用Java Card類,那么這當然不是問題。 換句話說,你可以放心地忽略它。
Q2:轉換器為什么要尋找這條路徑? 我將E:\\ ToCompile目錄指定為類目錄,但它將我指定的路徑與我的程序包名稱連接起來! 為什么?
原則上,未定義Java應用程序的源代碼文件夾結構。 但實際上,源位於反映包名稱的文件夾中。 所以一般來說,如果你的package語句讀取package package1.package2;
對於MyApplet
大多數工具都希望源代碼位於源文件夾中的package1/package2/MyApplet.java
中。 類文件以相同的方式放在文件夾中。 這適用於任何Java應用程序,而不僅僅是Java Card。
Q3:當我們打開用WinRAR一個.CAP文件,我們可以找到我們的包AID和我們的Applet的AID在
header.cap
和applet.cap
在它的文件。 在上面的步驟中,我只指定了我的包AID,那么如何在cap文件中分配Applet AID?
如果你沒有向轉換器提供Applet類/ AID,那么該包被認為是一個庫包(它是一個沒有自己狀態的包,只包含可供其他庫使用的代碼,當然也包括Applet) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.