1

I am using geotools in order to develop a custom GIS.

I tried to add a raster layer and a WMS based on the tutorials given by geotools. When i am running the project using the main class in Netbeans or Eclipse everything is working properly. However, when i tried to run the jar file from the cmd, the above functionalities crashes. Below you can see a screenshot which shows the error regarding the WMS.

I forgot to send that the project which i am working on is a Maven project.

Can you help me to understand where is the problem?

enter image description here

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-main</artifactId>
        <version>13.5</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-shapefile</artifactId>
        <version>13.5</version>
    </dependency>
    <dependency>
        <groupId>org.geotools</groupId>
        <artifactId>gt-swing</artifactId>
        <version>13.5</version>
    </dependency>
    <dependency>
    <groupId>org.geotools.xsd</groupId>
        <artifactId>gt-xsd-core</artifactId>
        <version>9.3</version>
    </dependency>
    <dependency>
        <groupId>org.geotools.xsd</groupId>
        <artifactId>gt-xsd-kml</artifactId>
        <version>9.3</version>
    </dependency>
    <dependency>
          <groupId>org.geotools.jdbc</groupId>
          <artifactId>gt-jdbc-postgis</artifactId>
          <version>13.5</version>
    </dependency>
    <dependency>
      <groupId>org.geotools</groupId>
      <artifactId>gt-geometry</artifactId>
      <version>${geotools.version}</version>
    </dependency>
    <dependency>
    <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1103-jdbc41</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>
</dependencies>
9
  • can you add your maven pom file - or a dependancy list. It looks like a missing jar
    – Ian Turton
    Jan 5 '16 at 20:35
  • I add the list of dependencies in the question below, Thanks a lot Jan 5 '16 at 23:21
  • and how did you build the jar you are running at the command line?
    – Ian Turton
    Jan 6 '16 at 8:38
  • I build the project using netbeans and then i am trying to run the jar which is located in the "target folder" from the command line. (java -jar nameJar.jar) Jan 6 '16 at 9:20
  • I also tried to build the project from cmd, however the problem is the same Jan 6 '16 at 12:31
1

The problem you are facing is answered in the GeoTools FAQ - basically you need to combine all the required dependency jars into your executable jar so that users don't need to worry about the classpath. Normally this is pretty easy to achieve using the assembly plugin in maven, however when using GeoTools' datastores (and other factories) this will fail as only the last found SPI file in META-INF/services will be saved.

So you need to use the maven shade plugin to combine the services files into a single larger file that will work in the combined jar.

You need to add a block like the following to your maven pom file:

      <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>
                          <!-- This bit sets the main class for the executable jar as you otherwise -->
                          <!-- would with the assembly plugin                                       -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                              <manifestEntries>
                                  <Main-Class>org.geotools.demo.Quickstart</Main-Class>
                              </manifestEntries>
                          </transformer>
                          <!-- This bit merges the various GeoTools META-INF/services files         -->
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                      </transformers>
                  </configuration>
              </execution>
          </executions>
      </plugin>

You'll need to change the main class name to match your program of course, and then run mvn package:shade to create the new jar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.