Setting Up LWJGL with IntelliJ IDEA

From LWJGL
Jump to: navigation, search

This tutorial will run you through the steps you'll need to get LWJGL setup with the Intellij IDEA IDE.

First create a new project.

To have something to verify that the libraries are loaded correctly both at compile and runtime we can add some simple code that uses LWJGL

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Main {
	public Main() {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.create();
			
			while(!Display.isCloseRequested()) {
				Display.update();
			}
			
			Display.destroy();
		} catch(LWJGLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Main();
	}
}

Then we add the needed jar files by going to File -> Project Structure:

IntelliJ Project Structure Menu Option.png

In the Modules page, in the Dependencies tab, click on the green plus symbol and choose "JARs or directories":

IntelliJ Project Structure Dependencies View.png

Select the 'lwjgl.jar' and 'lwjgl_util.jar' JARs:

IntelliJ Add Jar View.png


We are now able to successfully build the code...

IntelliJ Run Main Menu Option.png


...but if you try to run it you will get an error saying:

 Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl(64) in java.library.path

IntelliJ UnsatisfiedLinkException.png


This happens because the native libraries are not found. This can be fixed by creating a new folder inside the project called 'libs' and copy the correct folder from the native folder within lwjgl (depending on your operating system).

IntelliJ Add Directory Menu Option.png

LWJGL Add Directory View.png


Copy the contents of one of these into 'libs'

LWJGL Natives Folder.png


Here is an example project structure with Windows natives:

IntelliJ Project Structure Windows.png


This folder must then be passed to the JVM as an argument when running the program. To do this, open the run configuration.

Note: You should run your application before doing this, unless you want to add the JVM argument globally. From the screenshot you can see that I only added the argument to the configuration for the class containing my main method.

IntelliJ Edit Configurations Menu Option.png

And set the VM options to:

 -Djava.library.path=libs/

IntelliJ Run Configurations VM Args.png



After doing this you should be able to run the application without any errors :)