Setting Up LWJGL with NetBeans

Revision as of 12:14, 28 January 2011 by JediTofu (Talk | contribs) (Ant build script link.)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


This tutorial will explain how to setup LWJGL with the NetBeans IDE.
Requirements: You should have NetBeans (and Java) installed and should be familiar with creating a new Java project in NetBeans and extracting Zip files.
Note: X.X anywhere denotes the version number of LWJGL that you are specifically downloading and setting up.

Downloading and Setting Up LWJGL

Here are the first steps that are required for any IDE.

  1. Create a dedicated folder for LWJGL and remember its location. This folder will be the permanent residence of LWJGL on your system. Optionally, name it "lwjgl-X.X" to denote it from earlier/later versions.
  2. Download LWJGL from here into the folder.
    • is the only one required.
    • (Recommended.) Download if you want to be able to see LWJGL's Javadoc within NetBeans.
    • (Recommended.) Download if you want to be able to navigate to LWJGL's source code within NetBeans.
    • (Optional.) Download if you want to be able to create Java Applets with LWJGL. This exceeds the scope of this tutorial; for more information, please go to the LWJGL Applet Tutorials.
  3. Extract and
    • does not need to be extracted.


Setting Up LWJGL in NetBeans

  1. Open up NetBeans.
  2. Go to Tools|Libraries in the main menu.
    • NetBeans1.png

  3. Click on the New Library button.
    • NetBeans2.png

  4. Type in LWJGL or LWJGL-X.X to distinguish it from earlier/later versions or any other name that you want for the Library Name.
    • NetBeans3.png

  5. Always make sure that the correct Library is selected; you don't want to accidentally modify another Library. Now select the Classpath tab for your newly created Library and then click the Add JAR/Folder... button.
    • NetBeans4.png

  6. Go to the folder where you extracted and then go into the jar folder. The only JARs that you really need are lwjgl.jar, lwjgl_util.jar, and jinput.jar. You can select multiple files by holding down the Ctrl key while you click so that you can do this in just one step.
    • NetBeans5.png

  7. The rest of these steps are optional.
  8. Select the Sources tab and then click the Add JAR/Folder... button. Next, go to the folder where you extracted and then go into the src folder. Select the generated and java folders.
    • Note: Advanced users. If you want to only select a Zip file for whatever reason, use the Ant build script here.
    • NetBeans6.png

  9. Select the Javadoc tab and then click the Add JAR/Folder... button. Finally, select wherever you put, and we are done. Phew. *Wipes sweat from forehead.*


Setting Up a Project to Use LWJGL in NetBeans

  1. Create a new Java Application Project in NetBeans from File|New Project... in the main menu.
  2. Add the created Library from #Setting Up LWJGL in NetBeans to your project. You can do this 3 different ways:
    • (Shown in image below). Right click your project's Libraries node and then select Add Library....
    • Right click your project's node and then select Properties. Then select the Libraries category.
    • Go to File|Project Properties in the main menu and then select the Libraries category.
    • NetBeans7.png

  3. Your project should now look like this:
    • NetBeans8.png

  4. Go to your project's properties. You can do this 2 different ways:
    • (Shown in image below). Right click your project's node and then select Properties.
    • Go to File|Project Properties in the main menu.
    • NetBeans9.png

  5. Finally, you need to tell NetBeans where the Native Libraries (i.e, DLL, JNILIB, DYLIB, SO files) are for your system so that the natives are linked when running.
    • Select the Run category and then type the following into the VM Options, replacing what is in bold to suit your system:
      -Djava.library.path=<lwjgl-X.X path>/native/<linux|macosx|solaris|windows>
      • Note: If your path has any spaces in it, you'll need to surround it in quotes like this:
        -Djava.library.path="My Spacey Folder/lwjgl-X.X/native/windows"
      • Note: This only means that you can run the program inside of NetBeans. To run it outside of NetBeans, run this command:
        java -Djava.library.path=<path> -jar <name>.jar
        For more information on distribution, please go to the General FAQ.
    • NetBeans10.png


Testing the Full Setup

  1. Add the following import to a class in your project:
    import org.lwjgl.LWJGLException;
  2. Right click on the import and then select Navigate|Go to Source. Make sure that the actual code is there and that nothing looks like the following:
    //compiled code
    throw new RuntimeException("Compiled Code");
  3. Right click on the import again and then select Show Javadoc. This should open up a new window/tab through your web browser with documentation explaining the code.
  4. Finally, try compiling, building, and running this simple test code:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.*;

import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

 * @author jediTofu
 * @see <a href="">LWJGL Home Page</a>
public class Main {
  public static final int DISPLAY_HEIGHT = 480;
  public static final int DISPLAY_WIDTH = 640;
  public static final Logger LOGGER = Logger.getLogger(Main.class.getName());

  private int squareSize;
  private int squareX;
  private int squareY;
  private int squareZ;

  static {
    try {
      LOGGER.addHandler(new FileHandler("errors.log",true));
    catch(IOException ex) {

  public static void main(String[] args) {
    Main main = null;
    try {
      System.out.println("down  - Shrink");
      System.out.println("up    - Grow");
      System.out.println("left  - Rotate left");
      System.out.println("right - Rotate right");
      System.out.println("esc   - Exit");
      main = new Main();
    catch(Exception ex) {
    finally {
      if(main != null) {

  public Main() {
    squareSize = 100;
    squareX = 0;
    squareY = 0;
    squareZ = 0;

  public void create() throws LWJGLException {
    Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH,DISPLAY_HEIGHT));
    Display.setTitle("Hello LWJGL World!");




  public void destroy() {
    //Methods already check if created before destroying.

  public void initGL() {
    //2D Initialization

  public void processKeyboard() {
    //Square's Size
    if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
    if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {

    //Square's Z
    if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
    if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {

  public void processMouse() {
    squareX = Mouse.getX();
    squareY = Mouse.getY();

  public void render() {

    //Draw a basic square
    glTranslatef(-(squareSize >> 1),-(squareSize >> 1),0.0f);
      glTexCoord2f(0.0f,0.0f); glVertex2f(0.0f,0.0f);
      glTexCoord2f(1.0f,0.0f); glVertex2f(squareSize,0.0f);
      glTexCoord2f(1.0f,1.0f); glVertex2f(squareSize,squareSize);
      glTexCoord2f(0.0f,1.0f); glVertex2f(0.0f,squareSize);

  public void resizeGL() {
    //2D Scene



  public void run() {
    while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
      if(Display.isVisible()) {
      else {
        if(Display.isDirty()) {
        try {
        catch(InterruptedException ex) {

  public void update() {
    if(squareSize < 5) {
      squareSize = 5;
    else if(squareSize >= DISPLAY_HEIGHT) {
      squareSize = DISPLAY_HEIGHT;

Back to Main Wiki Page