简体   繁体   English

使用 maven 依赖项执行 jar 文件时出错。 java.lang.NoClassDefFoundError: org/opengis/feature/type/Name

[英]Error executing jar file with maven dependencies. java.lang.NoClassDefFoundError: org/opengis/feature/type/Name

I'm exporting my java app into a single JAR file but when I try to execute it I always get the same error.我正在将我的 java 应用程序导出到单个 JAR 文件中,但是当我尝试执行它时,我总是遇到同样的错误。

java.lang.NoClassDefFoundError: org/opengis/feature/type/Name java.lang.NoClassDefFoundError: org/opengis/feature/type/Name

Apparently there is something wrong including the opengis dependencies.显然有一些问题,包括opengis依赖项。

I am using the shade plugin and the mvn package command to generate the JAR file.我正在使用shade插件和mvn package命令生成 JAR 文件。

Here I include my pom.xml :在这里我包括我的pom.xml

<?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>

  <groupId>org.geotools</groupId>
  <artifactId>DataEngine</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>DataEngine</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <geotools.version>23-SNAPSHOT</geotools.version>  
    <start-class>exe.Main</start-class>
  </properties>

  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>${geotools.version}</version>
    </dependency>
    <dependency>
       <groupId>org.geotools</groupId>
       <artifactId>gt-epsg-hsql</artifactId>
       <version>${geotools.version}</version>
    </dependency>
       <dependency>
           <groupId>org.geotools</groupId>
           <artifactId>gt-geotiff</artifactId>
           <version>${geotools.version}</version>
       </dependency>
       <dependency>
           <groupId>org.geotools</groupId>
           <artifactId>gt-image</artifactId>
           <version>${geotools.version}</version>
       </dependency>
       <dependency>
           <groupId>org.geotools</groupId>
           <artifactId>gt-wms</artifactId>
           <version>${geotools.version}</version>
       </dependency>
       <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-graphx_2.12</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.12</artifactId>
            <version>2.4.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-sql_2.12</artifactId>
            <version>2.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>3.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-service</artifactId>
            <version>3.1.2</version>
        </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.opengis</groupId>
        <artifactId>geoapi</artifactId>
        <version>3.0.1</version>
    </dependency>

  </dependencies>

 <repositories>
        <repository>
            <id>maven2-repository.dev.java.net</id>
            <name>Java.net repository</name>
            <url>http://download.java.net/maven/2</url>
        </repository>
        <repository>
            <id>osgeo</id>
            <name>Open Source Geospatial Foundation Repository</name>
            <url>http://download.osgeo.org/webdav/geotools/</url>
        </repository>
        <repository>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <id>boundless</id>
          <name>Boundless Maven Repository</name>
          <url>http://repo.boundlessgeo.com/main</url>
        </repository>
        <repository>
            <id>spark graphx</id>
            <name>spark graphx</name>
            <url>https://mvnrepository.com/artifact/org.apache.spark/spark-graphx</url>
        </repository>
        <repository>
            <id>spark hive</id>
            <name>spark hive</name>
            <url>https://mvnrepository.com/artifact/org.apache.spark/spark-hive</url>
        </repository>
        <repository>
            <id>spark sql</id>
            <name>spark sql</name>
            <url>https://mvnrepository.com/artifact/org.apache.spark/spark-sql</url>
        </repository>
         <repository>
            <id>jdbc hive</id>
            <name>jdbc hive</name>
            <url>https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc</url>
        </repository>
        <repository>
            <id>hive service</id>
            <name>hive service</name>
            <url>https://mvnrepository.com/artifact/org.apache.hive/hive-service</url>
        </repository>
         <repository>
            <id>mysql driver</id>
            <name>mysql driver</name>
            <url>https://mvnrepository.com/artifact/mysql/mysql-connector-java</url>
        </repository>
        <repository>
            <id>commons-loggin</id>
            <name>commons-loggin</name>
            <url>https://mvnrepository.com/artifact/commons-logging/commons-logging</url>
        </repository>
        <repository>
            <id>opengis</id>
            <name>opengis</name>
            <url>https://mvnrepository.com/artifact/org.opengis/geoapi</url>
        </repository>
    </repositories>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.6</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

 <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>exe.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </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>
                    <artifactSet>
                        <includes>
                            <include>junit:junit</include>
                            <include>org.geotools:gt-shapefile</include>
                            <include>org.geotools:gt-swing</include>
                            <include>org.geotools:gt-epsg-hsql</include>
                            <include>org.geotools:gt-geotiff</include>
                            <include>org.geotools:gt-image</include>
                            <include>org.geotools:gt-wms</include>
                            <include>org.apache.spark:spark-graphx_2.12</include>
                            <include>org.apache.spark:spark-hive_2.12</include>
                            <include>org.apache.spark:spark-sql_2.12</include>
                            <include>org.apache.hive:hive-jdbc</include>
                            <include>org.apache.hive:hive-service</include>
                            <include>mysql:mysql-connector-java</include>
                            <include>commons-logging:commons-logging</include>
                            <include>org.opengis:geoapi</include>
                        </includes>
                    </artifactSet>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>exe.Main</mainClass>
                        </transformer>
                </configuration>
            </execution>
        </executions>
    </plugin>

      </plugins>
    </pluginManagement>
  </build>
</project>

Here the full error trace which I get:这是我得到的完整错误跟踪:

Exception in thread "main" java.lang.NoClassDefFoundError: org/opengis/feature/type/Name
    at exe.Main.initialize(Main.java:119)
    at exe.Main.main(Main.java:86)
Caused by: java.lang.ClassNotFoundException: org.opengis.feature.type.Name
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

I would really appreciate any help/suggestion.我真的很感激任何帮助/建议。 I've been stack at this point for quite a long time but still not getting any solution.在这一点上我已经堆叠了很长时间,但仍然没有得到任何解决方案。

I've added this assembly plugin:我添加了这个程序集插件:

<plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>exe.Main</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> 
            <phase>package</phase> 
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

But is still not working但仍然无法正常工作

**SOLVED: ** I just had to add <sourceDirectory>src/com/foobar</sourceDirectory> to <build> and execute the mvn clean compile assembly:single command. **已解决:** 我只需将<sourceDirectory>src/com/foobar</sourceDirectory>添加到<build>并执行mvn clean compile assembly:single命令。

You should avoid the maven assembly plugin as it will cause you issues in the future.您应该避免使用 maven 程序集插件,因为它会在将来给您带来问题 GeoTools modules often define one or more files in its META-INF/services directory with the same names as files defined in other modules. GeoTools模块通常在其 META-INF/services 目录中定义一个或多个文件,其名称与其他模块中定义的文件相同。 The assembly plugin just copies files with the same name over the top of each other rather than merging their contents.程序集插件只是将具有相同名称的文件相互重叠复制,而不是合并它们的内容。

The good news is that the Maven shade plugin can be used instead and it will correctly merge the META-INF/services files from each of the GeoTools modules used by your application.好消息是可以使用Maven 阴影插件,它会正确合并应用程序使用的每个 GeoTools 模块中的 META-INF/services 文件。

See the FAQ for more details .有关详细信息,请参阅常见问题解答

And finally, please use a stable release unless you are explicitly testing the release candidate for us (22-RC for example, became useless on the release of 22.0).最后,请使用稳定版本,除非您明确为我们测试候选版本(例如 22-RC,在 22.0 版本中变得无用)。

There is another plugin if you want to build your entire app to a single jar: http://maven.apache.org/plugins/maven-assembly-plugin/如果您想将整个应用程序构建到单个 jar,还有另一个插件: http://maven.apache.org/plugins/maven-assembly-plugin/

Maven assembly also support various type of packaging. Maven 组件还支持各种类型的封装。 You can consider using this if you cannot find a way to fix your current problem.如果您找不到解决当前问题的方法,可以考虑使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Maven构建的运行jar会导致“java.lang.NoClassDefFoundError:org / rosuda / JRI / Rengine”错误 - Running jar built using Maven causes “java.lang.NoClassDefFoundError: org/rosuda/JRI/Rengine” error IntelliJ - Maven 添加外部 jar 文件但 java.lang.NoClassDefFoundError - IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError Java Jar文件的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError for Java Jar File Jar文件给出java.lang.NoClassDefFoundError - Jar file gives java.lang.NoClassDefFoundError Maven: 无法使用 Maven 将本地 jar 打包到最终 jar 中,错误为 java.lang.NoClassDefFoundError - Maven: can not package local jar into final jar with maven and the error is java.lang.NoClassDefFoundError (JAR 文件):java.lang.NoClassDefFoundError - (JAR file): java.lang.NoClassDefFoundError jar文件无法正常工作java.lang.NoClassDefFoundError - jar file not working java.lang.NoClassDefFoundError 终端中带有jar文件的java.lang.NoClassDefFoundError - java.lang.NoClassDefFoundError with jar file in terminal .jar文件无法打开。 JNI错误。 java.lang.NoClassDefFoundError:org / apache / commons / exec / ExecuteStreamHandler - Can't open .jar file. JNI error. java.lang.NoClassDefFoundError: org/apache/commons/exec/ExecuteStreamHandler “java.lang.NoClassDefFoundError:org/apache/maven/doxia/siterenderer/DocumentContent” - "java.lang.NoClassDefFoundError: org/apache/maven/doxia/siterenderer/DocumentContent"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM