I'm very new to using external Java libraries, and I'm starting with Slick2D which is built upon LWJGL. I wrote a simple StateBasedGame that just prints three options on a screen. It gives me an error though about some (jar?) file not being present (see below for reference).
This reminds me of the fact that most of the tutorials I come across were made one or two years ago when LWJGL 3 hadn't come out or wasn't stable yet. This means that they were all using LWJGL 2. The problem is, that a lot of the code that I try using LWJGL 3 doesn't work with the code that was originally written in LWJGL 2. Why is this? Would it be a good idea to switch back to LWJGL 2 if I wanted to learn SLick2D? Please help because simple programs that I write don't even compile.
Here's my code that doesn't work (at least in LWJGL 3):
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
public class Game extends StateBasedGame {
Menu menu;
public Game(String title) {
super(title);
menu = new Menu(0);
}
public static void main(String[] args) {
try {
AppGameContainer agc = new AppGameContainer(new Game("Text Based"));
agc.setDisplayMode(600, 600, false);
agc.start();
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void initStatesList(GameContainer container) throws SlickException {
System.out.println("InitStates");
addState(new Menu(0));
getState(menu.getID()).init(container, this);
enterState(menu.getID());
}
}
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Menu extends BasicGameState {
private int ID;
private int menu;
public Menu(int id) {
this.ID = id;
}
@Override
public void init(GameContainer container, StateBasedGame sbg) throws SlickException {
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
g.setColor(Color.white);
g.drawString("Higher or Lower", 50, 10);
g.drawString("1. Play Game", 50, 100);
g.drawString("2. High Scores", 50, 120);
g.drawString("3. Quit", 50, 140);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
}
@Override
public int getID() {
return ID;
}
}
Here is the error:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl64 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:87)
at org.lwjgl.Sys.<clinit>(Sys.java:117)
at org.lwjgl.opengl.Display.<clinit>(Display.java:135)
at org.newdawn.slick.AppGameContainer$1.run(AppGameContainer.java:39)
at java.security.AccessController.doPrivileged(Native Method)
at org.newdawn.slick.AppGameContainer.<clinit>(AppGameContainer.java:36)
at Game.main(Game.java:17)
LWJGL 3 is a complete rewrite of the library, so naturally most code from LWJGL 2 won't work with it. Slick2D still uses LWJGL 2 under the hood, which makes it incompatible with LWJGL 3.
The error you are getting is there because Slick2D calls the LWJGL 2 display functionality, which tries to load the lwjgl64.dll
native file. However, in LWJGL 3 the native libraries have been renamed (lwjgl32
and lwjgl
instead of lwjgl
and lwjgl64
), so it can't find it. Even if it did, loading dlls and jars of both versions at the same time (LWJGL 2 by Slick2D and LWJGL 3 by your code) most likely won't work well.
If you want to use Slick2D you have to use LWJGL 2. However, LWJGL 3 is already quite stable and comes with some helper libraries you can use to do common tasks like loading image files, so I advise you to at least take a look at it (it offers some nice features like multi-window support).