Spring启动,runnable jar无法加载package-info.class

时间:2017-05-04 11:54:08

标签: spring spring-boot jar executable-jar package-info

我们使用Spring引导和hibernate数据库映射。 实体包含映射为使用 package-info.java 文件定义的自定义类型的JSON列。

当我们从Eclipse IDE运行spring-project时,一切正常,我们可以调用我们的Web服务。

当我们生成可执行jar并尝试调用我们的Web服务时,会引发以下错误:

mai 04, 2017 1:35:00 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.RuntimeException: Unable to execute job Test] with root cause
java.lang.NoClassDefFoundError: BOOT-INF/classes/com/test/package-info (wrong name: com/test/package-info)
    at java.lang.ClassLoader.defineClass1(Native Method)

文件package-info.class进入jar BOOT-INF / classes / com / test / package-info 什么可能是错的?

感谢您的帮助

6 个答案:

答案 0 :(得分:3)

我通过禁用包扫描和Hibernate分类的自动检测解决了这个问题

<persistence-unit name="sqlProvider" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>     
    <class>com.example.package.dao</class>

    .........     
    <properties>
        <property name="exclude-unlisted-classes" value="true"/>
        <property name="hibernate.archive.autodetection" value="hbm" />
        .........
    </properties>
    .........
</persistence-unit>

答案 1 :(得分:0)

我不想禁用扫描,因为手动将所有类添加到persistence.xml不是一种选择。由于我们在项目中实际上没有为hibernate使用包级别注释,因此我创造了这个令人厌恶的东西:

package com.example;

import org.hibernate.boot.archive.scan.internal.ScanResultImpl;
import org.hibernate.boot.archive.scan.internal.StandardScanner;
import org.hibernate.boot.archive.scan.spi.ScanEnvironment;
import org.hibernate.boot.archive.scan.spi.ScanOptions;
import org.hibernate.boot.archive.scan.spi.ScanParameters;
import org.hibernate.boot.archive.scan.spi.ScanResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

public class HibernateHackScanner extends StandardScanner {

    private static final Logger log = LoggerFactory.getLogger(HibernateHackScanner.class);

    @Override
    public ScanResult scan(ScanEnvironment environment, ScanOptions options, ScanParameters parameters) {
        log.info("Hack-Scanner is active, annotated packages will not be found by this scanner.");
        ScanResult scan = super.scan(environment, options, parameters);
        return new ScanResultImpl(Collections.emptySet(), scan.getLocatedClasses(), scan.getLocatedMappingFiles());
    }
}

然后我在persistence.xml中启用了此自定义扫描程序:

<property name="hibernate.archive.scanner" value="com.example.HibernateHackScanner"/>

此扫描仪使用引擎盖下的标准扫描仪,只需丢弃所有带注释的boot_inf.classes等附加注释的包。如果你真的需要包级别注释,你需要比这个拐杖更复杂一点。但这对我有用_ \ _(ツ)_ /¯

答案 2 :(得分:0)

我使用cxf-codegen-plugin来生成entites。在我的情况下,排除package-info类解决了问题。我从项目文件中删除了package-info.class,将extraargs添加到pom文件中,如:

 <plugin>
                <groupId>org.apache.cxf</groupId>
                <artifactId>cxf-codegen-plugin</artifactId>
                <version>${cxf.version}</version>

                <executions>
                          <execution>
                            <id>mvn s</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
                                <sourceRoot>${basedir}/src/main/java</sourceRoot>
                                <wsdlOptions>
                                    <wsdlOption>
                                        <wsdl>${basedir}/src/main/resources/wsdl/YOURWSDL.wsdl</wsdl>
                                        <wsdlLocation>classpath:wsdl/YOURWSDL.wsdl</wsdlLocation>

                                        <bindingFiles>
                                            <bindingFile>${basedir}/src/main/resources/wsdl/bindings.xml</bindingFile>
                                        </bindingFiles>

                                    </wsdlOption>

                                    <wsdlOption>
                                        <wsdl>${basedir}/src/main/resources/wsdl/YOURWSDL.wsdl</wsdl>
                                        <!--<extraargs>-->
                                            <!--<extraarg>-impl</extraarg>-->
                                            <!--<extraarg>-server</extraarg>-->
                                        <!--</extraargs>&ndash;&gt;-->
                                    </wsdlOption>
                                </wsdlOptions>

                                <defaultOptions>
                                    <extraargs>
                                        <extraarg>-xjc-npa</extraarg>
                                    </extraargs>
                                </defaultOptions>

                            </configuration>
                            <goals>
                                <goal>wsdl2java</goal>
                            </goals>
                        </execution>
                </executions>
            </plugin>

和包文件:mvn clean install spring-boot:repackage -DskipTests。现在它工作正常。

答案 3 :(得分:0)

Nestor Fedyk解决方案帮助我解决了这个问题。

请找到以下代码,我已将解决方案添加到我的休眠配置文件中

“排除未列出的类” 添加了“ hibernate.archive.autodetection” 属性

  <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="rcsDataSource"/>
    <property name="persistenceUnitName" value="corePersistenceUnit"/>
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</prop>
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.cache.region.factory_class">xxxx.xxxxxxxx.xxxx</prop>
            <prop key="hibernate.cache.provider_class">xxx.xxxxxxxx.xxxx</prop>
            <prop key="hibernate.cache.use_structured_entries">false</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.default_batch_fetch_size">250</prop>            
            <prop key="hibernate.max_fetch_depth">5</prop>            
            <prop key="hibernate.jdbc.fetch_size">300</prop>
            <prop key="exclude-unlisted-classes">true</prop>
            <prop key="hibernate.archive.autodetection">"hbm"</prop>
        </props>
    </property>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

我希望这对您有帮助

答案 4 :(得分:0)

我设法对JanThomä的解决方案进行了修改:

package com.example;

import org.hibernate.boot.archive.scan.internal.PackageDescriptorImpl;
import org.hibernate.boot.archive.scan.internal.ScanResultImpl;
import org.hibernate.boot.archive.scan.internal.StandardScanner;
import org.hibernate.boot.archive.scan.spi.*;
import org.hibernate.boot.archive.spi.ArchiveDescriptorFactory;

import java.util.Set;
import java.util.stream.Collectors;

public class CustomHibernateScanner extends StandardScanner {

    private static final String SPRING_BOOT_JAR_PREFIX = "BOOT-INF.classes.";

    public CustomHibernateScanner() {
    }

    public CustomHibernateScanner(ArchiveDescriptorFactory value) {
        super(value);
    }

    @Override
    public ScanResult scan(ScanEnvironment environment, ScanOptions options, ScanParameters parameters) {
        ScanResult result = super.scan(environment, options, parameters);
        Set<PackageDescriptor> pkgDesc = result.getLocatedPackages().stream()
                .map(this::fixPackageDescriptor)
                .collect(Collectors.toSet());
        return new ScanResultImpl(pkgDesc,
                result.getLocatedClasses(),
                result.getLocatedMappingFiles());
    }

    private PackageDescriptor fixPackageDescriptor(PackageDescriptor pd) {
        if (pd.getName().startsWith(SPRING_BOOT_JAR_PREFIX)) {
            return new PackageDescriptorImpl(pd.getName().substring(SPRING_BOOT_JAR_PREFIX.length()), pd.getStreamAccess());
        }
        return pd;
    }

}

与原始答案一样,它已添加到persistence.xml中。它基本上删除了导致异常的程序包描述符名称中的前缀。 这样,它就可以工作,无需禁用软件包扫描。

答案 5 :(得分:-1)

您可以在外部jar中提取包含JAXB生成的类(即包含package-info类的包)的包。也将JAXB相关资源移到新项目中(例如,XSD文件)。

现在你可以将这样一个jar设置为生成exacutable jar的项目的依赖项。

这样,当我从命令行运行它时,我的Spring Batch / Spring Boot项目就可以工作了。我知道这不是一个最佳解决方案,但在我的情况下,它可以工作。