ear maven项目中如何解决java.lang.NoClassDefFoundError?

How to solve java.lang.NoClassDefFoundError in ear maven project?

环境:

我尝试从 Front 模块执行 Commons 模块中的方法。 Front 模块没有 jasper 报告依赖,并从它使用 jasper 报告库的 Commons 模块执行一个方法。

它是从 Jboss5.2 迁移过来的,它工作正常,但是对于 Jboss7.2 我有一些问题。

当我尝试从 Front 模块执行 ReportManager 方法(公共模块)时,它抛出 java.lang.NoClassDefFoundError:net/sf/jasperreports/engine/JRException

知道这是为什么吗?

前置模组pom.xml

<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>
    <parent>
        <groupId>es.new.for</groupId>
        <artifactId>for2</artifactId>
        <version>8.0.0</version>
    </parent>

    <artifactId>for-front</artifactId>
    <packaging>war</packaging>
    <name>for-front</name>

        <dependency>
            <groupId>es.new.for</groupId>
            <artifactId>for-commons</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>es.new.for</groupId>
            <artifactId>for-ejb</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--No dependency to jasperreport-->
        ...
    

公共模块 pom.xml

<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">
    <parent>
        <groupId>es.new.for</groupId>
        <artifactId>for2</artifactId>
        <version>8.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>for-commons</artifactId>
    <packaging>jar</packaging>
    <name>for-commons</name>
...
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports-fonts</artifactId>
            <version>4.0.0</version>
        </dependency>
        ...
    

ReportManager.java(公共模块)

public class ReportManager { ... public static byte[] compilaReport(byte[] report) 抛出 AppException { 尝试 { log.info("编译"); ByteArrayInputStream inputStream = new ByteArrayInputStream(报告);

        JasperReport compiled = JasperCompileManager.compileReport(inputStream);

        return SerializationUtils.serialize(compiled);
    } catch (Exception e) {
        log.error("compile report", e);
        throw new AppException("InformeService.compile: Problem with compiling report", new AppException("Report compile error", e));
    }
}

...

ExecutarInformeBean.java(前模块)

public void compileReport(Informe informe, Map<String, Object> parametresSend) {

    if (informe != null) {

        try {

            if (parametresSend == null)
                parametresSend = new HashMap<>();

            // añadimos subinformes
            List<Parametre> parametres = informe.getParametres();
            for (Parametre parametre : parametres) {
                if (parametre.getTipus().equalsIgnoreCase("SUBINFORME")) {
                    try {
                        if (parametre.getSubInformeCompilado() == null) {
**                            parametre.setSubInformeCompilado(ReportManager.compilaReport(parametre.getSubInformeOriginal()));//Exeption java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRException
                        }
**                        ReportManager.addParameter(parametresSend, parametre); //Exeption java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRException
                    } catch (AppException e) {
                        log.error("compileReport subinforme", e);
                    } catch (Exception e) {
                        log.error("compileReport subinforme", e);
                    }
                }
            }
            ...
        

错误

13:36:57,311 SEVERE [org.primefaces.application.exceptionhandler.PrimeExceptionHandler] (default task-1) net/sf/jasperreports/engine/JRException: java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRException
    at deployment.for2.ear.for-front.war//es.new.for.presentation.front.informe.ExecutarInformeBean.compileReport(ExecutarInformeBean.java:200)
    at deployment.for2.ear.for-front.war//es.new.for.presentation.front.informe.ExecutarInformeBean.executaReportDirecte(ExecutarInformeBean.java:134)
    at deployment.for2.ear.for-front.war//es.new.for.presentation.front.informe.ExecutarInformeBean.reportDirecto(ExecutarInformeBean.java:74)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)

您将范围声明为 provided,这意味着工件未打包到 EAR 中。

如果没有以任何其他方式提供,调用 JAR 将失败。