Hello Guest

Getting my first LWJGL demo to work

  • 4 Replies
  • 6712 Views
Getting my first LWJGL demo to work
« on: November 17, 2006, 16:44:04 »
Hi, I am trying to use this library but I am having some problems...

I am trying to compile and run the demo from this site http://gpwiki.org/index.php/OpenGL:Tutorials:Java:LWJGL:Introduction

I am using JCreator and have set it up following these instructions, http://lwjgl.org/installation.php


Anyways, I compile the code, it works fine. I try and run it, and I get this error.

Exception in thread "main" java.lang.NoClassDefFoundError
at Game.cleanup(Game.java:105)
at Game.main(Game.java:40)
Press any key to continue...

I am running the game with these parameters -classpath "$[ClassPath]" -Djava.library.path=C:\lwjgl-1.0beta3\native $[JavaClass]


Here is my code
Code: [Select]

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
 
/**
 * Basic game
 *
 * @author Name <email>
 * @version 1.0
 */
public class Game {
 
  /** Game title */
  public static final String GAME_TITLE = "My Game";
 
  /** Desired frame time */
  private static final int FRAMERATE = 60;
 
  /** Exit the game */
  private static boolean finished;
 
  /** Angle of rotating square */
  private static float angle;
 
  /**
   * Application init
   * @param args Commandline args
   */
  public static void main(String[] args) {
    boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
 
    try {
      init(fullscreen);
      run();
    } catch (Exception e) {
      e.printStackTrace(System.err);
      Sys.alert(GAME_TITLE, "An error occured and the game will exit.");
    } finally {
      cleanup();
    }
    System.exit(0);
  }
 
  /**
   * Initialise the game
   * @throws Exception if init fails
   */
  private static void init(boolean fullscreen) throws Exception {
    // Create a fullscreen window with 1:1 orthographic 2D projection (default)
    Display.setTitle(GAME_TITLE);
    Display.setFullscreen(fullscreen);
 
    // Enable vsync if we can (due to how OpenGL works, it cannot be guarenteed to always work)
    Display.setVSyncEnabled(true);
 
    // Create default display of 640x480
    Display.create();
  }
 
  /**
   * Runs the game (the "main loop")
   */
  private static void run() {
 
    while (!finished) {
      // Always call Window.update(), all the time - it does some behind the
      // scenes work, and also displays the rendered output
      Display.update();
 
      // Check for close requests
      if (Display.isCloseRequested()) {
finished = true;
      }
 
      // The window is in the foreground, so we should play the game
      else if (Display.isActive()) {
        logic();
        render();
        Display.sync(FRAMERATE);
      }
 
      // The window is not in the foreground, so we can allow other stuff to run and
      // infrequently update
      else {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
        }
        logic();
 
// Only bother rendering if the window is visible or dirty
        if (Display.isVisible() || Display.isDirty()) {
          render();
        }
      }
    }
  }
 
  /**
   * Do any game-specific cleanup
   */
  private static void cleanup() {
    // Close the window
    Display.destroy();
  }
 
  /**
   * Do all calculations, handle input, etc.
   */
  private static void logic() {
    // Example input handler: we'll check for the ESC key and finish the game instantly when it's pressed
    if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      finished = true;
    }
 
    // Rotate the square
    angle += 2.0f % 360;
  }
 
 
  /**
   * Render the current frame
   */
  private static void render() {
    // clear the screen
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
 
    // center square according to screen size
    GL11.glPushMatrix();
    GL11.glTranslatef(Display.getDisplayMode().getWidth() / 2, Display.getDisplayMode().getHeight() / 2, 0.0f);
 
      // rotate square according to angle
      GL11.glRotatef(angle, 0, 0, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex2i(-50, -50);
        GL11.glVertex2i(50, -50);
        GL11.glVertex2i(50, 50);
        GL11.glVertex2i(-50, 50);
      GL11.glEnd();
 
    GL11.glPopMatrix();
  }
 
 
}


Anyone know what I am doing wrong?

hmmmmmmm...
« Reply #1 on: November 17, 2006, 18:08:54 »
Make sure the classpath includes the location of the LWJGL .jar files.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Re: Getting my first LWJGL demo to work
« Reply #2 on: February 27, 2007, 21:18:30 »
Hey, I'm getting the exact same error. (Same code and everything)

Any chance you could go into more detail on the solution?  :)

A total newbie,
 - Raelifin

Re: Getting my first LWJGL demo to work
« Reply #3 on: February 28, 2007, 02:13:15 »
It's likely not finding the class Display, as Fool Running said you need to make sure that lwjgl.jar is in your runtime classpath.

Bill
the2bears - the indie shmup blog

Re: Getting my first LWJGL demo to work
« Reply #4 on: February 28, 2007, 07:16:20 »
Well, despite not understanding how to alter the classpath in JCreator, I switched to eclipse and got it working. ^_^

Thanks anyway,
 - Raelifin