使用 jar 但不在库本身时出错

Error when using jar but not in the library itself

我构建了一个 Java 库来在 IBM MQ 中发送消息。

当我在库项目上执行代码时,它工作正常。

但是,当我使用.jar进入另一个工具(JMeter)时,出现错误。

java.lang.NoSuchMethodError: com.ibm.mq.jmqi.JmqiFactory.getInstance(Lcom/ibm/mq/jmqi/JmqiThreadPoolFactory;Lcom/ibm/mq/jmqi/JmqiPropertyHandler;)Lcom/ibm/mq/jmqi/JmqiEnvironment;
    at com.ibm.msg.client.mqlight.MQLightComponent.getImplementationInfo(MQLightComponent.java:220) ~[mq-jms-8.0.0.3.jar:8.0.0.3 - p800-003-150615.2]
    at com.ibm.msg.client.commonservices.trace.Trace.getVersion(Trace.java:1692) ~[mq-jms-7.0.1.3.jar:?]
    at com.ibm.msg.client.commonservices.trace.Trace.createFFSTString(Trace.java:1650) ~[mq-jms-7.0.1.3.jar:?]
    at com.ibm.msg.client.commonservices.trace.Trace.ffstInternal(Trace.java:1536) ~[mq-jms-7.0.1.3.jar:?]
    at com.ibm.msg.client.commonservices.trace.Trace.ffst(Trace.java:1444) ~[mq-jms-7.0.1.3.jar:?]
    at com.ibm.msg.client.jms.JmsFactoryFactory.getInstance(JmsFactoryFactory.java:209) ~[mq-jms-7.0.1.3.jar:7.0.1.3 - k701-103-100812]
    at com.ibm.mq.jms.MQConnectionFactory.initialiseMQConnectionFactory(MQConnectionFactory.java:3325) ~[mq-jms-7.0.1.3.jar:7.0.1.3 - k701-103-100812]
    at com.ibm.mq.jms.MQConnectionFactory.<init>(MQConnectionFactory.java:274) ~[mq-jms-7.0.1.3.jar:7.0.1.3 - k701-103-100812]
    at my.package.MQ_Manager.createConnection(MQ_Manager.java:36) ~[my-jar.jar:?]
    at my.package.MQ_Manager.<init>(MQ_Manager.java:27) ~[my-jar.jar:?]
    at my.package.Producer.<init>(Producer.java:18) ~[my-jar.jar:?]
    at my.package.Request.sendRequest(Request.java:116) ~[my-jar.jar:?]
    at my.package.Request$sendRequest.call(Unknown Source) ~[?:?]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120) ~[groovy-all-2.4.16.jar:2.4.16]
    at Script1.run(Script1.groovy:14) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:321) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_31]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.1 r1853635]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) ~[ApacheJMeter_java.jar:5.1 r1853635]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) ~[ApacheJMeter_core.jar:5.1 r1853635]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) ~[ApacheJMeter_core.jar:5.1 r1853635]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) ~[ApacheJMeter_core.jar:5.1 r1853635]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) ~[ApacheJMeter_core.jar:5.1 r1853635]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_31]

对应代码:

MQConnectionFactory factory = new MQConnectionFactory();
factory.setHostName(properties.getProperty("HOST"));
factory.setPort(Integer.parseInt(properties.getProperty("PORT")));
factory.setChannel(properties.getProperty("CHANNEL"));
factory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
factory.setQueueManager(properties.getProperty("QUEUE_MANAGER"));
factory.setAppName(properties.getProperty("APP_NAME"));
Connection connection = factory.createConnection(properties.getProperty("APP_USER"), properties.getProperty("APP_PASSWORD"));
connection.start();
return connection;

这一行出现错误MQConnectionFactory factory = new MQConnectionFactory();

有什么想法吗?谢谢。

更新 1

创建 .jar 我 :

  1. 点击了 Export
  2. 已选择Runnable JAR file
  3. 已选择Extract required libraries into generated JAR

更新 2

此外,当我构建 Jar 时,我收到此警告。你觉得重要吗?

This operation repacks references libraries. Please review the licences associated with libraries you wish ti reference to make sure you are able to repack them using this application. Note also that this operation does not copy signature files from orignal libraies to the generated JAR file.

在与 OP 讨论了这个问题后,我继续自己验证了这个问题。 结果是它对我有用...

第 1 步: 创建了一个包含以下代码的 Maven 项目

package test;

import javax.jms.JMSException;
import com.ibm.mq.jms.MQConnectionFactory;

public class JmeterTest {

    public JmeterTest() {
    }

    public void test() throws JMSException {
        MQConnectionFactory factory = new MQConnectionFactory();
        factory.setAppName("myApp");
    }

    public static void main(String[] args) {
        System.out.println("test");
    }
}

第 2 步: 将其从 eclipse 导出为可运行的 jar 并复制到 JMeter (\lib\ext\)。

请注意,使用库处理 package required jars into generated jar 的导出不起作用。使用 Extract into generated jarCopy into subfolder(然后将子文件夹中的 jar 也复制到 \lib\ext)。

相关依赖项是:

com.ibm.mq.allclient-9.0.4.0.jar
bcokix-jdk15on-1.57.jar
bcprov-jdk15on-1.57.jar
javax.jms-api-2.0.1.jar

第 3 步: 启动 JMeter 并创建了一个带有 JSR223 Sampler.

的线程组
import test.JmeterTest;
new JmeterTest().test();

然后开始测试。没有发生错误。

第 4 步:

您可以直接(添加依赖项后)将所需代码添加到脚本面板中,而不是导出库:

import javax.jms.JMSException;
import com.ibm.mq.jms.MQConnectionFactory;

MQConnectionFactory factory = new MQConnectionFactory();
factory.setAppName("myApp");

同样有效


结论:来自其他依赖项对 jmeter 类路径的干扰是问题的最可能原因。