Java Card Converter : class does not belong to package\

Java Card Converter : class does not belong to package\ … here is a solution to the problem.

Java Card Converter : class does not belong to package\

I’m creating a very simple Java Card applet (version 2.2.2), but I’m new to Java cards and I don’t know how to convert a .class file to a .cap file. I’m using a converter.bat file.

I’ve managed to compile a single .java file into a .class with eclipse….

I tried moving my applet to the default package and removing the package from top of the code….

I tried googling this issue but without success….

I tried compiling my code with the command line with the -target and -source compatibility options

I followed the tutorial without success: https://lavamunky.wordpress.com/2010/03/28/java-card-prog-compile/

When I run:

.\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

It throws me an error message: error: class token does not belong to package\

My Java applet:

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);
    }
}

Solution

You only need to put a package declaration in your Java source file (you should always do this, don’t ignore the default package warning).

For example:

package com.myname.javacard.test;

Works fine.

The Java Card applet is the part of the loading module, which basically consists of a normal Java package. This package/load module will also be assigned AID (hexadecimal code in converter). However, this is not possible if you use the default package, and if you do not have a package declaration, use the default package.


Also, you must make sure that your class file is located correctly. Make sure they are under the folder .\Token. Probably you should simply remove the \Token from the class directory (it’s a directory, not a .class file) or remove the entire -classdir completely.

If you are unsure, specify the full path to the folder in quotation marks or double quotation marks.

Related Problems and Solutions