Building and running lwjgl program from terminal

别等时光非礼了梦想. 提交于 2021-02-07 08:26:24

问题


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.


回答1:


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();
    }

}



回答2:


you are missing some .jar in classpath. You can add with -classpath but take care about extra signs at linux. Very probably you will need those at launch too:

java -classpath ~/Downloads/lwjgl-2.8.3/jar/lwjgl.jar:~/Do... -Djava.library.path=~/Downloads/lwjgl-2.8.3/native/linux HelloWorld

I am not sure about the ~ sign, try to put there absolute path.




回答3:


Try to make a complie.sh with javac command, set all .jars what you need in classpath and add all your classes, what you need to compile. This should result one or more .class files in output directory. At that point you know you hava compiled your "project".

After this you can launch with java, but launching with java has 2 modes:

java -jar yourJarName.jar

for this you need to jar all your classes into jar file and add all required other jars in the maifest file, or you will extract all required jars and embed into your jars they classes

and the other one is like what you have tried

java -classpath your1.jar your2.jar toon of classes in claaspath com. yourpackage.MainClass

For exactly how to proceeed, pls search for "compile java project in command line Linux" and "run java application from command line linux"



来源:https://stackoverflow.com/questions/12332381/building-and-running-lwjgl-program-from-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!