This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 30th, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,299 Users
  • 2,269,776 Discussions
  • 7,916,824 Comments

Discussions

InaccessibleObjectException: Unable to make field private static long sun.security.smartcardio.PCSC

User_QKUPX
User_QKUPX Member Posts: 1 Green Ribbon

When ı used below code to thread to detect smartcard terminal:

public class TerminalWaiter extends Thread {
    public void run() {
        while(!Thread.interrupted()) {
            //System.out.println("termianlwaiter");
            CardTerminals cardTerminals = TerminalFactory.getDefault().terminals();
            try {
                cardTerminals.list();
                init();

            } catch (CardException e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                continue;
            }

            synchronized (cardTerminals) {
                resetPCSC();

                try {
                    cardTerminals.waitForChange();
                } catch (Exception e) {
                    resetPCSC();
                }
                resetPCSC();

            }
        }
    }

    void resetPCSC() {

        try {
            Class pcscterminal = Class.forName("sun.security.smartcardio.PCSCTerminals");
            Field contextId = pcscterminal.getDeclaredField("contextId");
            contextId.setAccessible(true);

            if(contextId.getLong(pcscterminal) != 0L)
            {
                Class pcsc = Class.forName("sun.security.smartcardio.PCSC");
                Method SCardEstablishContext = pcsc.getDeclaredMethod(
                        "SCardEstablishContext",
                        new Class[] {Integer.TYPE }
                );
                SCardEstablishContext.setAccessible(true);

                Field SCARD_SCOPE_USER = pcsc.getDeclaredField("SCARD_SCOPE_USER");
                SCARD_SCOPE_USER.setAccessible(true);

                long newId = ((Long)SCardEstablishContext.invoke(pcsc,
                        new Object[] { Integer.valueOf(SCARD_SCOPE_USER.getInt(pcsc)) }
                )).longValue();
                contextId.setLong(pcscterminal, newId);

                // Then clear the terminals in cache
                TerminalFactory factory = TerminalFactory.getDefault();
                CardTerminals terminals = factory.terminals();
                Field fieldTerminals = pcscterminal.getDeclaredField("terminals");
                fieldTerminals.setAccessible(true);
                Class classMap = Class.forName("java.util.Map");
                Method clearMap = classMap.getDeclaredMethod("clear");

                clearMap.invoke(fieldTerminals.get(terminals));
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

    }
}


I got the  Unable to make field private static long sun.security.smartcardio.PCSCTerminals.contextId accessible: module java.smartcardio does not "opens sun.security.smartcardio" to module gov.tubitak.gmsdtool error

Is there anyone who can help me?