Eclipse Maven项目ClassNotFoundException在运行时

时间:2019-01-18 15:56:13

标签: java eclipse maven geotools

我想尝试使用GeoTools进行CRS转换。我在Eclipse中创建了一个新的Maven项目并设置了依赖项。 pom.xml文件包含以下内容:

<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>geotools-homolosine</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <geotools.version>21-SNAPSHOT</geotools.version>
    </properties>

  <dependencies>
        <dependency>
          <groupId>org.geotools</groupId>
          <artifactId>gt-referencing</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-epsg-extension</artifactId>
          <version>${geotools.version}</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>
    </repositories>

</project>

用于 clean install 的Maven命令均已成功完成,并且Eclipse在 Maven Dependencies 文件夹中显示了下载的库。

然后我创建了一个简单程序:

import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

public class Transformation 
{
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        CoordinateReferenceSystem WORLDCRS = DefaultGeographicCRS.WGS84;
        CoordinateReferenceSystem crs;
        MathTransform transform = null;

        try 
        {
            crs = CRS.decode("3035");
            transform = CRS.findMathTransform(WORLDCRS, crs, true);
        } 
        catch (Exception e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(transform.toWKT());
        System.out.println(transform.getClass().getCanonicalName());
    }
}

Java编辑器未报告任何错误或警告。但是,当我运行程序(右键单击> 运行方式> Java应用程序)时,控制台显示以下内容:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/opengis/referencing/crs/CoordinateReferenceSystem
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.opengis.referencing.crs.CoordinateReferenceSystem
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    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)
    ... 7 more

以某种方式在运行时无法访问Maven依赖项。可能是什么不对?

2 个答案:

答案 0 :(得分:1)

此错误是由build文件中缺少pom.xml部分引起的。除其他外,本节确定了项目的主要类别。显然,如果没有这些信息,Eclipse将无法正确启动项目。这是与上述项目匹配的build部分:

<build>
 <plugins>
  <plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>exec-maven-plugin</artifactId>
   <version>1.1.1</version>
   <executions>
    <execution>
     <phase>test</phase>
     <goals>
      <goal>java</goal>
     </goals>
     <configuration>
      <mainClass>org.ldesousa.Transformation</mainClass>
     </configuration>
    </execution>
   </executions>
  </plugin>
 </plugins>
</build>

请注意,我必须将主体包含在软件包中(在这种情况下为org.ldesousa)。

答案 1 :(得分:0)

我使用您的代码创建了一个测试Maven项目,然后将其从Eclipse中导入,将3035更改为EPSG:3035后,一切工作正常。

刷新Maven项目以确保下载了jar时,我会检查您是否处于离线模式。