Jdk1.6 connects to the pcsc card reader in java to send the APDU command and pcscapdu
The Package javax. smartcardio provided by java operation card reader jdk1.6 can be directly connected to the pcsc card reader and interact with the card reader for instructions.
Package javax. smartcardio details
Http://docs.oracle.com/javase/6/docs/jre/api/security/smartcardio/spec/javax/smartcardio/package-summary.html
Java code example
/**
* Connectpcsc. java
*/
Import java. util. List;
Import javax. smartcardio. CardTerminal;
Import javax. smartcardio. TerminalFactory;
Public class connectpcsc {
/**
* Main function, listing all the PC/SC readers connected to your PC
*/
Public static void main (String [] args ){
// Show the list of available terminals
TerminalFactory factory = TerminalFactory. getDefault ();
// List of readers (empty)
List <CardTerminal> terminals;
Try {
// Get list of readers form the terminal
Terminals = factory. terminals (). list ();
// Print list of readers to the console.
System. out. println (terminals. toString ());
} Catch (Exception e ){
// Print Stack-Trace in case of an error
E. printStackTrace ();
}
}
}
Import java. util. List;
Import javax. smartcardio. Card;
Import javax. smartcardio. CardChannel;
Import javax. smartcardio. CardException;
Import javax. smartcardio. CardTerminal;
Import javax. smartcardio. CommandAPDU;
Import javax. smartcardio. ResponseAPDU;
Import javax. smartcardio. TerminalFactory;
Public class GetStatus {
Public static void main (String [] args ){
TerminalFactory factory = TerminalFactory. getDefault ();
Try {
List <CardTerminal> terminal = factory. terminals (). list ();
System. out. println ("terminal:" + terminal );
CardTerminal ter = terminal. get (0 );
// Establish a connection with the card
Card card = ter. connect ("T = 0 ");
System. out. println ("card:" + card );
CardChannel channel = card. getBasicChannel ();
ResponseAPDU r = channel. transmit (new CommandAPDU (160,242, 0, 0, 22); // A0 F2 00 00 16
System. out. println ("response:" + r. toString ());
For (int j = 0; j <8; j ++ ){
System. out. print (Integer. toHexString (int) (r. getData () [j] + 256) % 256) + "");
}
System. out. print ("\ n ");
For (int j = 0; j <card. gettarget(). getBytes (). length; j ++ ){
System. out. print (Integer. toHexString (int) (card. getrecognition (). getBytes () [j] + 256) % 256) + "");
}
// Disconnect
Card. disconnect (false );
} Catch (CardException e ){
// TODO Auto-generated catch block
System. out. println ("connection erro, or card not inserted .");
}
}
}
How to configure the environment for java jdk160
According to your java installation path, the environment variable configuration is as follows:
Java_home: D: \ Program Files \ Java \ jdk1.6.0 jdk; note that
Classpath: Add: D: \ Program Files \ Java \ jre1.6.0 jre \ bin; also, add a semicolon. Do not change other content in classpath, if a java path exists, delete it. Note that multiple variables are separated by semicolons.
Path: Add D: \ Program Files \ Java \ jdk1.6.0 jdk \ bin; with semicolon.
After setting, you can use a command prompt to test whether the environment variable settings are correct. Enter java in the corresponding path, or if javac displays command usage information, it indicates that the configuration is successful.
In addition, we recommend that you configure the environment variables under the system environment variables. the difference between the two environment variables is that the system environment variables are valid under any circumstances, and the user environment variables are valid only when the user logs in...
Open Environment Variables. Right-click my computer and choose Properties> advanced> environment variables. The above one is the user environment variable. The following one is the system environment variable. select the corresponding variable name and click Edit to modify it.
JDK configuration in Java: "JAVA_HOME c: \ Program Files \ Java \ jdk160_11" is not successful in JDK 6. Is this file a problem?
JAVA_HOME does not need to be referenced in other places. You can directly configure c: \ Program Files \ Java \ jdk1.6.0 _ 11 in the path variable.