Roger
Roger

Reputation: 11

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.lang.NoClassDefFoundError: org/opengis/feature/type/Name

Apparently there is something wrong including the opengis dependencies.

I am using the shade plugin and the mvn package command to generate the JAR file.

Here I include my 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.

Upvotes: 0

Views: 685

Answers (3)

Ian Turton
Ian Turton

Reputation: 10976

You should avoid the maven assembly plugin as it will cause you issues in the future. GeoTools modules often define one or more files in its META-INF/services directory with the same names as files defined in other modules. 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.

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).

Upvotes: 2

Roger
Roger

Reputation: 11

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.

Upvotes: 1

Hưng Chu
Hưng Chu

Reputation: 1052

There is another plugin if you want to build your entire app to a single jar: http://maven.apache.org/plugins/maven-assembly-plugin/

Maven assembly also support various type of packaging. You can consider using this if you cannot find a way to fix your current problem.

Upvotes: 0

Related Questions