使用自定义模块运行应用程序时出现NoClassDefFoundError [英] NoClassDefFoundError when running application with custom module

查看:26
本文介绍了使用自定义模块运行应用程序时出现NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Mule SDK构建了一个Mule 4模块,该模块在内部使用com.googlecode.json-simple。要创建项目,我使用了Maven原型org.mule.extensions:mule-extensions-archetype-maven-plugin:1.2.0,如https://docs.mulesoft.com/mule-sdk/1.1/getting-started

中所述

我可以构建项目并将其包含在AnyPoint Studio 7.8中,并可以利用Mule应用程序中的操作,我可以在UI中配置它们,更新其属性,但是每当我运行该应用程序时,都会出现java.lang.NoClassDefFoundError失败,因为它找不到库。

我应该执行哪些附加配置以确保库在运行时可用?

错误:

ERROR 2021-04-09 06:33:48,410 [WrapperListener_start_runner] org.mule.runtime.module.deployment.internal.DefaultArchiveDeployer: Failed to deploy artifact [project1]
org.mule.runtime.deployment.model.api.DeploymentException: Failed to deploy artifact [project1]
Caused by: java.lang.NoClassDefFoundError: com/google/gson/JsonObject
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_232]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_232]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_232]
    at org.mule.runtime.module.extension.internal.util.IntrospectionUtils.lambda$getMethodsStream$14(IntrospectionUtils.java:923) ~[mule-module-extensions-support-4.3.0-20210119.jar:4.3.0-20210119]
    at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:269) ~[?:1.8.0_232]
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175) ~[?:1.8.0_232]
    at java.util.HashMap$KeySpliterator.forEachRemaining(HashMap.java:1556) ~[?:1.8.0_232]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482) ~[?:1.8.0_232]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472) ~[?:1.8.0_232]

模块的pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>org.mule.extension</groupId>
    <artifactId>mule-basic-extension</artifactId>
    <version>1.0.1-SNAPSHOT</version>
    <packaging>mule-extension</packaging>
    <name>My Extension</name> 

    <parent>
        <groupId>org.mule.extensions</groupId>
        <artifactId>mule-modules-parent</artifactId>
        <version>1.1.3</version>
    </parent>

    <dependencies>
        <dependency>
           <groupId>com.googlecode.json-simple</groupId>
           <artifactId>json-simple</artifactId>
           <version>1.1.1</version>
        </dependency>
     </dependencies>

</project>

编辑: 操作类

public class MyOperations {
  Logger logger = LoggerFactory.getLogger(MyOperations.class);


  /**
   * Example of an operation that uses the configuration and a connection instance to perform some action.
   */
  @MediaType(value = ANY, strict = false)
  public String retrieveInfo(@Config MyConfiguration configuration, @Connection MyConnection connection){
    return "Using Configuration [" + configuration.getConfigId() + "] with Connection id [" + connection.getId() + "]";
  }

  /**
   * Example of a simple operation that receives a string parameter and returns a new string message that will be set on the payload.
   */
  @MediaType(value = ANY, strict = false)
  public String sayHi(String person) {
    return buildHelloMessage(person);
  }

  /**
   * Returns a JSON representation of the input
   */
  @Throws(ExecuteErrorsProvider.class)
  @MediaType(value = MediaType.APPLICATION_JSON, strict = false)
  public String parseInput(String record) {
    JsonObject jsonObject = new JsonObject();

    jsonObject.addProperty("key", record);

    return jsonObject.toString();
  }


  /**
   * Private Methods are not exposed as operations
   */
  private String buildHelloMessage(String person) {
    return "Hello " + person + "!!!";
  }
}

推荐答案

原来我在项目中使用的JSON库有问题。

我替换了此依赖项

<dependency>
  <groupId>com.googlecode.json-simple</groupId>
  <artifactId>json-simple</artifactId>
  <version>1.1.1</version>
</dependency>

使用此命令,我可以创建返回JSON的操作

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>

这篇关于使用自定义模块运行应用程序时出现NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆