从终端构建并运行lwjgl程序 [英] Building and running lwjgl program from terminal

查看:51
本文介绍了从终端构建并运行lwjgl程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译并运行一个使用LWJGL的非常基本的程序:

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

并且:

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

但是这些都不起作用.他们俩都抱怨找不到LWJGLException类定义.

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

我正在运行Linux,但是没有使用IDE(例如Eclipse或Netbeans).我不想使用一个,我希望能够从终端运行.

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.

推荐答案

以下内容可在我的Windows机器上使用,但我已将shell命令改编为linux格式(冒号与分号):

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

设置目录结构,例如:

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

编译:

从您的外壳中,导航到包含HelloWorld.java的父目录,然后键入以下内容:

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

javac -cp .:lib/* HelloWorld.java

-cp 指定可以在.(当前目录)和任何 jar 文件放在 lib/下.请注意,您可以手动指定 .jar 文件,即 -cp.:lib/lwjgl.jar:lib/jinput.jar 等,但是 * (我相信仅限 java 1.6+)是指定目录中所有 jar 的快捷方式.

-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.

运行:

现在从父目录运行以下命令:

Now run the following command from the parent directory:

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

同样, -cp 指定可以在当前目录以及/lib 下的任何jar中找到已编译的 .class 文件.目录. -Djava.library.path = 指定可以在哪里找到您的本机文件.请注意,您没有在 native 前面放置前导/.通过省略开头的/,您将告诉Java native 目录是相对于当前工作目录的子目录.如果不小心包含了/,它将把native视为绝对目录,这可能不是您想要的.

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.

为当前工作目录之外的本机文件指定位置是完全可以接受的.为此,您必须提供绝对位置,例如Windows上的绝对位置:

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

这应该是在没有IDE或构建脚本的情况下启动和运行所需的全部内容!

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

最终通知

HelloWorld.java 表现不佳(屏幕锁定,您必须强制关闭该过程).尝试以下代码(从网络上的多个来源改编而成,进行了一些修改以适合此示例,但主要是我自己的努力, not ),以代替 HelloWorld.java .享受吧!

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

}

这篇关于从终端构建并运行lwjgl程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆