Executable Jar: JMSCC0091: The provider factory fo

2019-07-29 07:08发布

问题:

I encountered this very weird problem while working on using JMSTemplate to connect to IBM Websphere MQ. The code runs perfectly fine in Eclipse, but when I use shade plugin to package the project and all the dependencies into an Uber jar, I got this error while executing the jar file:

com.ibm.msg.client.jms.DetailedJMSException: JMSCC0091: The provider factory for  connection type 'com.ibm.msg.client.wmq' could not be loaded.
        at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:57)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:437)
        at com.ibm.msg.client.commonservices.j2se.NLSServices.createException(NLSServices.java:319)
        at com.ibm.msg.client.commonservices.nls.NLSServices.createException(NLSServices.java:233)
        at com.ibm.msg.client.jms.internal.JmsErrorUtils.createException(JmsErrorUtils.java:109)
        at com.ibm.msg.client.jms.internal.JmsFactoryFactoryImpl.getInstance(JmsFactoryFactoryImpl.java:172)
        at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.setProviderFactory(JmsConnectionFactoryImpl.java:167)
        at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.createConnection(JmsConnectionFactoryImpl.java:254)
        at com.ibm.mq.jms.MQConnectionFactory.createCommonConnection(MQConnectionFactory.java:6215)
        at com.ibm.mq.jms.MQQueueConnectionFactory.createQueueConnection(MQQueueConnectionFactory.java:120)

Here is my relevant Java code to read the keystore and truststore file as InputStream and the error happened when the method createQueueConnection was called.

InputStream keystoreInput = Thread.currentThread().getContextClassLoader().getResourceAsStream(keystoreFile);
InputStream truststoreInput = Thread.currentThread().getContextClassLoader().getResourceAsStream(trustStoreFile);
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] keyPassword = keystorePassword.toCharArray();
keyStore.load(keystoreInput, keyPassword);

KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyFactory.init(keyStore, keyPassword);

KeyManager[] keyManagers = keyFactory.getKeyManagers();
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());

char[] trustPassword = trustStorePassword.toCharArray();
trustStore.load(truststoreInput, trustPassword);

TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustFactory.init(trustStore);

TrustManager[] trustManagers = trustFactory.getTrustManagers();
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(keyManagers, trustManagers, null);
SSLContext.setDefault(sslContext);

MQQueueConnectionFactory cf = ctx.getBean("mqIdsConnectionFactory", MQQueueConnectionFactory.class);
cf.setSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();

I checked my xml file and all the IBM MQ dependencies are included correctly, and I can find the class files in the uber jar as well. It has been bothering me in the past few days and if anybody knows how to fix it, I would really appreciate your input and suggestions.

<dependency>
<groupId>com.ibm</groupId>
    <artifactId>com.ibm.mq</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>com.ibm.mqjms</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>com.ibm.mq.headers</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>com.ibm.mq.jmqi</artifactId>
    <version>7.5.0</version>
</dependency>        
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>dhbcore</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.jsse2</groupId>
    <artifactId>ibmjsseprovider2</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.security</groupId>
    <artifactId>ibmjcefw</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.crypto</groupId>
    <artifactId>ibmjceprovider</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.security</groupId>
    <artifactId>ibmcertpathprovider</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.security</groupId>
    <artifactId>ibmpkcs</artifactId>
    <version>7.5.0</version>
</dependency>
<dependency>
    <groupId>com.ibm</groupId>
    <artifactId>connector</artifactId>
    <version>7.5.0</version>
</dependency>

回答1:

Make sure to not extract the dependencies inside the jar but just include them as stated in the comments. If you export from within eclipse, there is a radio option to choose from.