2012-11-22 59 views
3

下面的代碼從https://sites.google.com/site/justinscsstuff/jogl-tutorial-2爲什麼這個Java OpenGL(JOGL)程序不能運行?

import java.awt.Frame; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import javax.media.opengl.*; 
import javax.media.opengl.awt.GLCanvas; 

public class SimpleScene { 
    public static void main(String[] args) { 
     GLProfile glp = GLProfile.getDefault(); 
     GLCapabilities caps = new GLCapabilities(glp); 
     GLCanvas canvas = new GLCanvas(caps); 

     Frame frame = new Frame("AWT Window Test"); 
     frame.setSize(300, 300); 
     frame.add(canvas); 
     frame.setVisible(true); 

     // by default, an AWT Frame doesn't do anything when you click 
     // the close button; this bit of code will terminate the program when 
     // the window is asked to close 
     frame.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
    } 
} 

它編譯沒有任何問題的拍攝,但是當我使用

java SimpleScene 

我收到以下錯誤

C:\Users\Mitc\Drive\Google Drive\Game\Display>java SimpleScene 
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLCapabilitiesImmutable 
     at java.lang.Class.getDeclaredMethods0(Native Method) 
     at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 
     at java.lang.Class.getMethod0(Unknown Source) 
     at java.lang.Class.getMethod(Unknown Source) 
     at sun.launcher.LauncherHelper.getMainMethod(Unknown Source) 
     at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) 
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLCapabilitiesImmutable 
     at java.net.URLClassLoader$1.run(Unknown Source) 
     at java.net.URLClassLoader$1.run(Unknown Source) 
     at java.security.AccessController.doPrivileged(Native Method) 
     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) 
     ... 6 more 

任何想法,以什麼會出錯?

回答

4

正如你已經編譯的文件與JOGL jar文件,你只需要確保你在運行時在你的classpath這些文件:

java -cp gluegen-rt.jar;jogl-all.jar;. SimpleScene 
+2

他使用JOGL 2,在這個版本中沒有jogl.jar(jogl.jar來自JOGL 1)。通常,使用jogl-all.jar,並且gluegen-rt.jar也必須位於類路徑中。請按照這些說明:http://jogamp.org/wiki/index.php/Setting_up_a_JogAmp_project_in_your_favorite_IDE#Compile_and_run_your_project_from_the_command_line – gouessej

+0

@gouessej好點,更正! :) – Reimeus

+0

感謝您的快速解決。 JOGL 2不再強制設置Java庫路徑,因爲它自動從JAR(jogl-all-natives - *。jar和gluegen-rt-natives - *。jar)中提取本機庫當且僅當它們在與其非本地對應目錄相同的目錄(jogl-all.jar和gluegen-rt.jar)。 – gouessej

1

您需要使用JOGL < V2.3,因爲包名稱已更改。這裏有一個鏈接v2.2.4 jars。出於某種原因,gluegen-rt.jar和jogl-all.jar實際上是包含真正罐子的zip文件,因此請牢記這一點。

我意識到這是不正確的答案當時OP最初發布,但我遇到了同樣的錯誤,並認爲我會幫助下一個人。

+0

原始海報可以簡單地用「com.jogamp」替換「javax.media」,使他的代碼可以使用最新版本而不是使用過時的代碼。包裝到ZIP文件中的JAR文件的問題來自某些瀏覽器,爲了「安全」目的將JAR包裝到ZIP中,而不是下載包含所有JAR的7z存檔或使用包含單個JAR的胖JAR文件的FAT存檔。 – gouessej

+0

關於包裝,是的,這是真的這個問題。這更多的是對錯誤消息的響應,這可能發生在用戶沒有源代碼的代碼中。 –

+0

@gouessej至於壓縮,它有一個.jar擴展名,這很奇怪。 –

相關問題