JAR文件

时间:2017-08-10 13:38:41

标签: java apache maven lucene maven-shade-plugin

我正在尝试在JAR文件中运行我的Java Maven项目。它在IntelliJ IDEA 2017上运行,但是,当通过JAR文件运行它时,它不会运行。显示以下错误:

Exception in thread "main" java.util.ServiceConfigurationError: Cannot instantiate SPI class: org.apache.lucene.codecs.lucene62.Lucene62Codec
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:82)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:51)
    at org.apache.lucene.util.NamedSPILoader.<init>(NamedSPILoader.java:38)
    at org.apache.lucene.codecs.Codec$Holder.<clinit>(Codec.java:47)
    at org.apache.lucene.codecs.Codec.getDefault(Codec.java:143)
    at org.apache.lucene.index.LiveIndexWriterConfig.<init>(LiveIndexWriterConfig.java:121)
    at org.apache.lucene.index.IndexWriterConfig.<init>(IndexWriterConfig.java:151)
    at com.hrforecast.skillextraction.LuceneIndexManager.buildLuceneIndex(LuceneIndexManager.java:54)
    at com.hrforecast.skillextraction.SkillExtractionModule.getJobsSearcher(SkillExtractionModule.java:129)
    at com.hrforecast.skillextraction.SkillExtractionModule.run(SkillExtractionModule.java:60)
    at com.hrforecast.skillextraction.Main.main(Main.java:28)
Caused by: java.lang.IllegalArgumentException: An SPI class of type org.apache.lucene.codecs.PostingsFormat with name 'Lucene50' does not exist.  You need to add the corresponding JAR file supporting this SPI to your classpath.
  The current classpath supports the following names: [IDVersion]
    at org.apache.lucene.util.NamedSPILoader.lookup(NamedSPILoader.java:116)
    at org.apache.lucene.codecs.PostingsFormat.forName(PostingsFormat.java:112)
    at org.apache.lucene.codecs.lucene62.Lucene62Codec.<init>(Lucene62Codec.java:167)
    at org.apache.lucene.codecs.lucene62.Lucene62Codec.<init>(Lucene62Codec.java:82)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at org.apache.lucene.util.NamedSPILoader.reload(NamedSPILoader.java:72)
    ... 10 more

我在其他stackoverflow问题中看到了类似的问题,例如What causes err ' A SPI class of type lucene.codecs.Codec name 'Lucene42',但这也没有解决我的问题。

我的pom.xml如下。如您所见,我也尝试使用Resource Transformers,但它没有改变任何内容。

<?xml version="1.0" encoding="UTF-8"?>
<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>

...
<dependencies>

    <!-- Spring framework -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.2.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.5.0</version>
    </dependency>


    <!-- Spring data mongodb -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-mongodb</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-core -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>6.6.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-analyzers-common -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>6.6.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-queryparser -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>6.6.0</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>1.26</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <!--<plugin>-->
            <!--<artifactId>maven-compiler-plugin</artifactId>-->
            <!--<version>3.0</version>-->
            <!--<configuration>-->
                <!--<source>1.6</source>-->
                <!--<target>1.6</target>-->
            <!--</configuration>-->
        <!--</plugin>-->
        <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-eclipse-plugin</artifactId>-->
            <!--<version>2.9</version>-->
            <!--<configuration>-->
                <!--<downloadSources>true</downloadSources>-->
                <!--<downloadJavadocs>true</downloadJavadocs>-->
            <!--</configuration>-->
        <!--</plugin>-->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com..Main</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

任何帮助或指导都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我的解决方案是使用Maven Shade plugin,因为我使用IntelliJ,问题在于我如何创建JAR文件。

而不是使用&#39;工件&#39;来创建它。在IntelliJ中构建,我使用&#39; mvn&#39;创建了JAR文件。终端中的工具。确保您的JAVA_HOME设置正确。我不得不再次使用apt-get安装openjdk8。最后,在目标目录中创建了正确的JAR文件。

有关如何使用Maven Shade创建JAR文件的更多详细信息,请参阅here

(此解决方案适用于Lucene 6.6.0,Windows 10 bash)。