Java:maven 项目中出现 NoClassDefFoundError?

标签 java render lwjgl display

我已经开始使用 Maven 在 Codenvy IDE 中使用 OpenGL 开发 3D java 游戏引擎。我已配置为运行 Java 8,并在 pom.xml 中设置了 LWJGL 所需的依赖项。但是,当我运行测试窗口时,我收到 NoClassDefFoundError。我不确定为什么会发生这种情况,考虑到 Maven 配置为将所有 .Jar 依赖项存储到一个文件中,因此访问它们不应该是一个问题。所有文件都已正确下载到我的 Codenvy 项目中的 External Packages 目录中。

这是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>engineTester.First3DEngine</groupId>
   <artifactId>First3DEngine</artifactId>
   <version>1.5.7</version>
   <packaging>jar</packaging>

  <dependencies>  
    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl_util</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>org.lwjgl.lwjgl</groupId>
      <artifactId>lwjgl_util_applet</artifactId>
      <version>2.8.4</version>
    </dependency>

    <dependency>
      <groupId>com.typesafe.slick</groupId>
      <artifactId>slick_2.10</artifactId>
      <version>3.1.1</version>
    </dependency>
    <dependency>
      <groupId>java3d</groupId>
      <artifactId>vecmath</artifactId>
  <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <!-- Build an executable JAR -->
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <argLine>-Xmx1024m</argLine>
          <archive>
            <manifest>
          <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
              <addClasspath>true</addClasspath>
              <classpathPrefix>lib/</classpathPrefix>
              <mainClass>engineTester.MainGameLoop</mainClass>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
   </build>
</project>

这就是我遇到问题的地方:

package engineTester;

import org.lwjgl.opengl.Display;

import renderEngine.DisplayManager;
import util.Console;

public class MainGameLoop {
    public static void main(String[] args){
        new Console();
        DisplayManager.createDisplay();  <-- line which throws the error
        while(!Display.isCloseRequested()){
            //game logic
            //render
            DisplayManager.updateDisplay();
        }

        DisplayManager.closeDisplay();

    }
}

DisplayManager.createDisplay()方法:

public static void createDisplay(){
    ContextAttribs attribs = new ContextAttribs(3,2);
    attribs.withForwardCompatible(true);
    attribs.withProfileCore(true);

    try{
        Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
        Display.create(new PixelFormat(), attribs);
    } catch (LWJGLException e){
        e.printStackTrace();
    }

    GL11.glViewport(0,0,WIDTH,HEIGHT);
}

最后是完整的错误:

Exception in thread "main" java.lang.NoClassDefFoundError:     org/lwjgl/LWJGLException
    at engineTester.MainGameLoop.main(MainGameLoop.java:11)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 1 more

感谢您的帮助!

最佳答案

您可以使用 maven-assemble-plugin 制作一个可以自行运行的 fat jar,也可以使用 maven 运行您的程序,以便将编译时 jar 添加到您的类路径中。

使用maven运行:

mvn exec:java -Dexec.mainClass="engineTester.MainGameLoop"

使用程序集插件:

<build>
    <plugins>
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.5.5</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>engineTester.MainGameLoop</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这将创建一个 jar,并将 -jar-with-depenccies 添加到目标目录中的名称中。然后可以使用 java -jar jarfile 运行。

关于Java:maven 项目中出现 NoClassDefFoundError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34403279/

相关文章:

java - 如何在Java中模拟一个站点?

java - Intellij 14,Lambda 表达式有问题,尽管没有编译器错误但仍显示错误

java - 如何最好地用Java实现 war 迷雾?

java - 如何停止渲染不可见的面孔

java - 我原来的 Java 字体程序 - 使用不同字母时倾斜?

java - 实例化作业时 Quartz Scheduler 错误

java - 将值传递给小程序

ruby-on-rails - 如何在保持 ruby​​ on Rails 中的顺序的同时从哈希渲染 json 响应?

java - 为什么我的纹理在 slick util 中显示得如此奇怪?

java - 高度图上 2 个顶点之间的空间