Why won't this Java OpenGL (JOGL) program run?

2019-02-25 08:34发布

问题:

The following code was taken from https://sites.google.com/site/justinscsstuff/jogl-tutorial-2

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);
            }
        });
    }
}

It compiles without any problems, but when I use

java SimpleScene

I get the following error

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

Any ideas as to what is going wrong?

回答1:

As you have already compiled the file with JOGL jar files, you just need to make sure that you have these files in your classpath at runtime:

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


回答2:

You need to use JOGL < v2.3 because the package names changed. Here's a link to the v2.2.4 jars. For some reason gluegen-rt.jar and jogl-all.jar were actually zip files which contained the real jars, so keep that in mind.

I realize this isn't the correct answer given when the OP originally posted, but I encountered the same error and thought I'd help out the next person.



标签: java opengl jogl