Search code examples
javalinuxterminallwjgl

Building and running lwjgl program from terminal


I'm trying to compile and run a very basic program that uses LWJGL:

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

public class HelloWorld {
     public static void main (String args[]) {
         try {
             Display.setTitle("Hello World");
             Display.create();
         } catch (LWJGLException e) {
             e.printStackTrace();
         }

         while (!Display.isCloseRequested()) {
             try {
                 Thread.sleep(100);
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
    }
}

I managed to compile it using:

javac -classpath ~/Downloads/lwjgl-2.8.3/jar/lwjgl.jar:~/Downloads/lwjgl-2.8.3/jar/lwjgl_util.jar:~/Downloads/lwjgl-2.8.3/jar/jinput.jar HelloWorld.java

But now I can't run it... I tried:

java HelloWorld

And:

java -Djava.library.path=~/Downloads/lwjgl-2.8.3/native/linux HelloWorld

But none of those works. Both of them complain that LWJGLException class definition was not found.

I am running Linux, and I am not using an IDE such as Eclipse or Netbeans. I don't want to use one, I want to be able to run from terminal.


Solution

  • The following works on my Windows machine, but I've adapted the shell commands for linux formatting (colons vs. semi-colons):

    Set up a directory structure as such:

    /HelloWorld.java
    /lib/jwjgl.jar
    /lib/jinput.jar
    /lib/jwjgl_util.jar
    /native/linux/... (all your native files)
    

    Compile:

    From your shell, navigate to the parent directory containing HelloWorld.java, and type the following:

    javac -cp .:lib/* HelloWorld.java
    

    -cp specifies that the .java and .class files to compile your program can be found within both . (the current directory) and any jar file under lib/. Note that you could manually specify the .jar files, i.e. -cp .:lib/lwjgl.jar:lib/jinput.jar etc, but * (java 1.6+ only, I believe) is a shortcut to specify all jars in a directory.

    Run:

    Now run the following command from the parent directory:

    java -cp .:lib/* -Djava.library.path=native/linux HelloWorld
    

    Again, -cp specifies that your compiled .class files can be found in the current directory and within any jars under the /lib directory. -Djava.library.path= specifies where your native files can be found. Note that you did not put a leading / in front of native. By omitting the leading /, you're telling java that the native directory is a subdirectory relative to the current working directory. If you accidentally include the /, it will treat native as an absolute directory, which is probably not what you want.

    It's perfectly acceptable to specify a location for the native files outside of the current working directory. To do so, you'll have to provide the absolute location, which on Windows would be, for example:

    -Djava.library.path=C:\jwjgl-2.8.4\native\windows
    

    That should be all you need to get up-and-running without an IDE or build script!

    Final Note

    The HelloWorld.java, as written, behaves poorly (the screen locks up and you must force-close the process). Try the following code (adapted from multiple source across the web, with minor modifications to suit this example, but primarily not of my own effort), as a replacement for HelloWorld.java. Enjoy!

    import org.lwjgl.LWJGLException;
    import org.lwjgl.opengl.Display;
    import org.lwjgl.opengl.DisplayMode;
    import org.lwjgl.opengl.GL11;
    import org.lwjgl.input.Keyboard;
    
    public class HelloWorld{
        public void start() {
            try {
                Display.setDisplayMode(new DisplayMode(640, 480));
                Display.create();
            } catch (LWJGLException e) {
                e.printStackTrace();
                System.exit(0);
            }
    
            // Init OpenGL
            GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GL11.glOrtho(-3.2, 3.2, -2.4, 2.4, -1, 1);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
    
            boolean quit = false;
    
            while (!quit) {         
                // Clear the screen.
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    
                // Begin drawing
                GL11.glBegin(GL11.GL_TRIANGLES);
    
                    // Top & Red
                    GL11.glColor3f(1.0f, 0.0f, 0.0f);
                    GL11.glVertex2f(0.0f, 1.0f);
    
                    // Right & Green
                    GL11.glColor3f(0.0f, 1.0f, 0.0f);
                    GL11.glVertex2f(1.0f, 1.0f);
    
                    // Left & Blue
                    GL11.glColor3f(0.0f, 0.0f, 1.0f);
                    GL11.glVertex2f(1.0f, -1.0f);
    
                GL11.glEnd();
    
                Display.update();
    
                if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
                    quit = true;
            }
    
            Display.destroy();
        }
    
        public static void main(String args[]) {
            HelloWorld application = new HelloWorld();
            application.start();
        }
    
    }