I have initialized the SunPKCS11 provider by:
Provider provider = new sun.security.pkcs11.SunPKCS11(\"path_to_pkcs11.cfg\"); Security.addProvider(provider);
Your are getting this exception because once the program get executed for the first time the values get retained with the process id in java.So to resolve this make it as a jar file and run as batch file