title: Smart Card detection with java
body: <p>i'm using the following code to test whether there is a smart card connected to computer or not</p>
  
  <pre><code>public static boolean verifySmartCardPresence() {
      boolean cardPresent = false;
      try {
          TerminalFactory tf = TerminalFactory.getDefault();
          List&lt;CardTerminal&gt; terminals = tf.terminals().list();
          for (CardTerminal terminal : terminals) {
              if (terminal.isCardPresent()) {
                  System.out.println(terminal);
                  cardPresent = true;
              }
          }
          return cardPresent;
      } catch (CardException ex) {
          LOGGER.error(ex.getMessage(), ex);
          return false;
      }
  }
  </code></pre>
  
  <p>it works like a charm but when i'm unplug the smartcard and replug it without closing the program, it does not retrieve it anymore, it gives me exception just like there is no smart  card connected
  what is the problem ?<br>
  [UPDATE]<br>
  this is the error log : </p>
  
  <pre><code>[10-04-2018 16:14:28.801] [ERROR][c.m.utils.CertificateUtils.verifySmartCardPresence(138)] - list() failed 
  javax.smartcardio.CardException: list() failed
  at sun.security.smartcardio.PCSCTerminals.list(Unknown Source) ~[na:1.8.0_161]
  at javax.smartcardio.CardTerminals.list(Unknown Source) ~[na:1.8.0_161]
  at com.mar.utils.CertificateUtils.verifySmartCardPresence(CertificateUtils.java:130) ~[mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at com.mar.ws.impl.SignWSImpl.signData(SignWSImpl.java:58) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
  at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
  at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
  at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:152) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:406) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:350) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.internal.Errors.process(Errors.java:315) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.internal.Errors.process(Errors.java:297) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.internal.Errors.process(Errors.java:267) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:320) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:769) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1125) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1059) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.Server.handle(Server.java:497) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:248) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:610) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:539) [mar-1.0-SNAPSHOT-jar-with-dependencies.jar:na]
  at java.lang.Thread.run(Unknown Source) [na:1.8.0_161]
  Caused by: sun.security.smartcardio.PCSCException: SCARD_E_SERVICE_STOPPED
  at sun.security.smartcardio.PCSC.SCardListReaders(Native Method) ~[na:1.8.0_161]
  </code></pre>