Java Card Converter:类不属于包

时间:2018-01-06 16:22:31

标签: java smartcard javacard

我正在创建一个非常简单的Java Card Applet(版本2.2.2),但我是Java卡的新手,我无法弄清楚如何将.class文件转换为.cap文件。我正在使用converter.bat文件。

我已经设法使用eclipse编译单个.java文件到.class ...

我尝试将我的applet移动到默认包中,并从代码顶部删除包...

我试过谷歌搜索这个问题,没有任何成功......

我尝试使用-target和-source兼容性选项

在命令行中编译我的代码

我遵循了本教程,没有取得任何成功:https://lavamunky.wordpress.com/2010/03/28/java-card-prog-compile/

我跑的时候:

.\converter.bat -applet 0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00:0x00 Token -classdir .\Token\ -exportpath %JC_HOME%\api_export_files \ 0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08:0x09:0x00 1.0

它会向我抛出一条错误消息:error: class Token does not belong to package \.

我的java小程序:

import javacard.framework.*;

public class Token extends Applet {
    /* constants declaration */
    // code of CLA byte in the command APDU header
    final static byte Amblar_CLA =(byte)0xb0;

    // codes of INS byte in the command APDU header
    final static byte SET_TOKEN = (byte) 0x30;
    final static byte GET_TOKEN = (byte) 0x40;

    private short token;

    /**
     * Installs this applet.
     * 
     * @param bArray
     *            the array containing installation parameters
     * @param bOffset
     *            the starting offset in bArray
     * @param bLength
     *            the length in bytes of the parameter data in bArray
     */
    public static void install(byte[] bArray, short bOffset, byte bLength) {
        new Token(bArray, bOffset, bLength);
    }

    /**
     * Only this class's install method should create the applet object.
     */
    private Token(byte[] bArray, short bOffset, byte bLength) {
        token = 0x00;
        register();
    }

    public boolean select() {
        return true;
    }

    public void process(APDU apdu) {
         byte[] buffer = apdu.getBuffer();

        if ((buffer[ISO7816.OFFSET_CLA] == 0) && (buffer[ISO7816.OFFSET_INS] == (byte)(0xa4)))
            return;

        if (buffer[ISO7816.OFFSET_CLA] != Amblar_CLA)
          ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

        switch (buffer[ISO7816.OFFSET_INS]) {
            case SET_TOKEN: 
                setToken(apdu);
                break;
            case GET_TOKEN: 
                getToken(apdu);
                break;
            default: ISOException.throwIt (ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    private void setToken(APDU apdu) {
        byte[] buffer = apdu.getBuffer();

        byte byteRead = (byte)(apdu.setIncomingAndReceive());

        if (byteRead != 1)
            ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        byte newToken = buffer[ISO7816.OFFSET_CDATA];

        token = (short)newToken;
    }

    private void getToken(APDU apdu) {
        byte[] buffer = apdu.getBuffer();

        short le = apdu.setOutgoing();

        if ( le < 2 ) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);

        apdu.setOutgoingLength((byte)2);
        buffer[0] = (byte)(token >> 8);
        buffer[1] = (byte)(token & 0xff);
        apdu.sendBytes((short)0, (short)2);
    }
}

1 个答案:

答案 0 :(得分:3)

您只需要在Java源文件中放置一个包声明(就像您一直应该的那样,不要忽略默认包警告)。

例如:

package com.myname.javacard.test;

会正常工作。

Java Card applet是加载模块的一部分,它基本上由普通的Java包组成。此包/加载模块也将分配AID(转换器中的十六进制代码)。但是,如果使用默认包,则这是不可能的,如果没有包声明,则使用默认包。

此外,您必须确保正确找到您的类文件。确保它们位于文件夹 .\Token下。您可能只需从类目录中删除\Token(它是一个目录,而不是.class文件),或者完全删除整个-classdir

如果不确定,请在引号或双引号内指定文件夹的完整路径。