2015-12-21 99 views
0

我已經開始使用Maven在Codenvy IDE中使用OpenGL開發3D Java遊戲引擎。我配置了運行Java 8,並且我已經設置了所需的pom.xml的依賴關係。但是,當我運行測試窗口時,我得到一個NoClassDefFoundError。我不確定爲什麼發生這種情況,考慮到Maven被配置爲將所有.Jar依賴關係存儲到一個文件中,因此訪問它們不應該成爲問題。所有文件已正確下載到我的Codenvy項目的External Packages目錄中。Java:Maven項目中的NoClassDefFoundError?

這裏是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 

感謝您的幫助!

+0

清楚地說明了LWJGL中的類,或者實際上並不在運行時類路徑中,所以很顯然,jar導出中的某些內容並不像您期望的那樣發生。 Maven jar插件不會像你所期望的那樣創建一個胖罐子。在zip工具中打開生成的jar並親自查看。 – Gimby

+0

@Gimby所以我將如何去設置'.jar'文件在運行時類路徑上?使用''? –

+0

你正在運行哪些maven目標? AFAIK,那個jar插件配置實際上不會做任何事情,除非你配置一個執行(或明確地調用一個jar-plugin目標)。 – rmlan

回答

0

你可以使用maven-assembly-plugin來製作一個可以自行運行的fat jar,或者你可以使用maven運行你的程序,這樣編譯時jar就會被添加到你的classpath中。

要使用maven運行:

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

要使用Assembly插件:

<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-with-depencencies添加到名稱在目標目錄中。那可以使用java -jar jarfile運行。

+0

感謝您的回覆,但是問題似乎是我無法真正控制/我不知道何去控制maven的執行方式。沒有一個終端可以輸入'mvn exec:java -Dexec.mainClass =「engineTester.MainGameLoop」',因爲我使用的是codenvy IDE(這是一個基於雲的IDE,用於瀏覽器,完全獨立到系統變量)。那麼Codenvy怎麼樣呢? –

+0

對不起,我以前從未使用過CodeEnvy。從理論上來看,人們可以用Build - > Custom Maven Build ..和其他人通過用maven命令替換runner屬性文件中的CMD來運行一些maven命令,但在我的測試中,這兩個似乎都非常有限/有問題。對於這種應用程序,使用桌面IDE進行開發似乎更容易,或者只需在自己的計算機上運行Maven和文本編輯器即可。如果你真的需要使用CodeEnvy,使用程序集插件可能是你最好的選擇。 – WillShackleford

相關問題