Problem creating a Provider from a file inside the jar file
Hello everyone. I need help. I am trying to add a PKCS11 provider, but the dll file for this provider need
to be inside of the jar file. I looked for how to find a file inside the jar, its path. That part of the
program seems to work, becasuse when I print on screen the path, it seems correct. But I don't think that
it work for the part when I need the path to load the provider, because the path is like this :
C:\Projects\Programita.jar!\programita\xxxxxxxxx.dll
When I run the program from the jar file, it throws java.lang.reflect.InvocationTargetException on the
sentence :
pkcs11Provider = (Provider) pkcs11Constr.newInstance(confStream);
Anyone knows how can I load a provider using a dll file from inside the jar file?.
Thanks in advance :P.
PS: Sorry, but I can't give you the name of the dll file.
package programita;
import java.io.*;
import java.net.*;
import java.security.*;
import java.security.cert.*;
import java.util.*;
publicclass Main{
publicstaticvoid main(String[] args){
URL urlFileName = Main.class.getResource("/programita/xxxxxxxxx.dll");
String filePath =null;
System.out.println(urlFileName);
try{
filePath = URLDecoder.decode(urlFileName.getFile(),"UTF-8");
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}catch (Exception ee){
ee.printStackTrace();
}
/*These sentences are to modify the path because filePath before these sentences
looks like this : file:/C:/Projects/Programita.jar!/programita/xxxxxxxxx.dll */
filePath = filePath.replaceFirst("/","");
filePath = filePath.replace("/","\\");
filePath = filePath.replaceFirst("file:","");
System.out.println(filePath);
// First configure the Sun PKCS#11 provider. It requires a stream (or file)
// containing the configuration parameters - "name" and "library".
String pkcs11ConfigSettings =
"name = SmartCard\n" +"library = " + filePath;
byte[] pkcs11ConfigBytes = pkcs11ConfigSettings.getBytes();
ByteArrayInputStream confStream =new ByteArrayInputStream(pkcs11ConfigBytes);
// Instantiate the provider dynamically with Java reflection
Provider pkcs11Provider =null;
try{
Class sunPkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
java.lang.reflect.Constructor pkcs11Constr = sunPkcs11Class.getConstructor(
java.io.InputStream.class);
pkcs11Provider = (Provider) pkcs11Constr.newInstance(confStream);
Security.addProvider(pkcs11Provider);
}catch (Exception e){
System.out.println(e);
}
//To list the Providers, and see if it was added
Provider[] provv = Security.getProviders();
for (int i=0;i<provv.length;i++ ){
System.out.println(provv[i].toString());
}
}
}
Here I add what the program shows:
C:\Projects\Programita>java -jar Programita.jar
C:\Projects\Programita\Programita.jar!\programita\xxxxx.dll
java.lang.reflect.InvocationTargetException
SUN version 1.6
SunRsaSign version 1.5
SunJSSE version 1.6
SunJCE version 1.6
SunJGSS version 1.0
SunSASL version 1.5
XMLDSig version 1.0
SunPCSC version 1.6
SunMSCAPI version 1.6
C:\Projects\Programita>

