【问题标题】:LWJGL: failed to load a libraryLWJGL: 加载库失败
【发布时间】:2016-08-13 16:10:52
【问题描述】:

一直在尝试在 linux 上使用“lwjgl”,当我从终端运行编译后的代码时遇到了问题。我正在使用 lwjgl 3 的稳定版本。

我从网站下载了 lwjgl.jar 并运行命令javac -cp lwjgl.jar: Main.java 它可以很好地编译代码。然后我运行:java -cp lwjgl.jar: Main 之后它会抛出这个错误;

[LWJGL] Failed to load a library. Possible solutions:
    a) Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
    b) Add the JAR(s) containing the shared libraries to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
Exception in thread "EndlessRunner" java.lang.UnsatisfiedLinkError: Failed to locate library: liblwjgl.so
    at org.lwjgl.system.Library.loadSystemRelative(Library.java:100)
    at org.lwjgl.system.Library.loadSystem(Library.java:71)
    at org.lwjgl.system.Library.<clinit>(Library.java:43)
    at org.lwjgl.system.MemoryAccess.<clinit>(MemoryAccess.java:17)
    at org.lwjgl.system.Pointer.<clinit>(Pointer.java:22)
    at org.lwjgl.glfw.GLFW.<clinit>(GLFW.java:562)
    at Main.init(Main.java:31)
    at Main.run(Main.java:78)
    at java.lang.Thread.run(Thread.java:745)

我不确定我是否错过了一些我需要的文件,或者我是否完全错误地处理了这个问题。这是我正在使用的代码,只是我在网上找到的一些代码,我正在使用它作为测试。

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.ByteBuffer;
import org.lwjgl.glfw.GLFWVidMode;

public class Main implements Runnable{

private Thread thread;
public boolean running = true;

private long window;

private int width = 1200, height = 800;

public static void main(String args[]){
    Main game = new Main();
    game.start();
}

public void start(){
    running = true;
    thread = new Thread(this, "EndlessRunner");
    thread.start();
}

public void init(){
    // Initializes our window creator library - GLFW 
    // This basically means, if this glfwInit() doesn't run properlly
    // print an error to the console
    if(glfwInit() != true){
        // Throw an error.
        System.err.println("GLFW initialization failed!");
    }

    // Allows our window to be resizable
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    // Creates our window. You'll need to declare private long window at the
    // top of the class though. 
    // We pass the width and height of the game we want as well as the title for
    // the window. The last 2 NULL parameters are for more advanced uses and you
    // shouldn't worry about them right now.
    window = glfwCreateWindow(width, height, "Endless Runner", NULL, NULL);

    // This code performs the appropriate checks to ensure that the
    // window was successfully created. 
    // If not then it prints an error to the console
    if(window == NULL){
        // Throw an Error
        System.err.println("Could not create our Window!");
    }

    // creates a bytebuffer object 'vidmode' which then queries 
    // to see what the primary monitor is. 
    //ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    // Sets the initial position of our game window. 
    glfwSetWindowPos(window, 100, 100);
    // Sets the context of GLFW, this is vital for our program to work.
    glfwMakeContextCurrent(window);
    // finally shows our created window in all it's glory.
    glfwShowWindow(window);
}

public void update(){
    // Polls for any window events such as the window closing etc.
    glfwPollEvents();
}

public void render(){
    // Swaps out our buffers
    glfwSwapBuffers(window);
}

@Override
public void run() {
    // All our initialization code
    init();
    // Our main game loop
    while(running){
        update();
        render();
        // Checks to see if either the escape button or the
        // red cross at the top were pressed.
        // if so sets our boolean to false and closes the
        // thread.
        if(glfwWindowShouldClose(window) == true){
            running = false;
        }
    }
}

}

如果你们能提供任何帮助,我们将不胜感激。

谢谢。

【问题讨论】:

  • 一般来说,这个错误很常见,你的描述也不是很具体。正如消息所说:它找不到本机库。请注意,除了 JAR,您需要liblwjgl.so(包含在您从网站下载的ZIP 包中)。对于第一次测试,您可以将此liblwjgl.so 放入项目的主目录中,看看它是否有效。如果可行,您可以阅读更多关于本机库加载和java.library.path 的信息。
  • Set -Djava.library.path or -Dorg.lwjgl.librarypath to the directory that contains the shared libraries.
  • 谢谢,我会读到这个。

标签: java lwjgl


【解决方案1】:

我只能说使用 NetBeans 8 运行 LWJGL 3,但我也遇到了同样的错误。我发现的问题与最初设置 LWJGL 时需要添加到“类路径”选项卡的“本机”jar 文件有关。这样做的原因是它使 LWJGL 能够自动查找本机 jar。然后在您的 VM 设置下,您需要将其设置为:

-Djava.library.path="Path to where you extracted JAR files"

仅当路径名包含空格时才包含引号

【讨论】:

    【解决方案2】:

    您可以将lwjgl.jar 添加到模块路径,将lwjgl-native-\*.jar 添加到classpath。其他也是。例如:将lwjgl-opengl.jar添加到模块路径,将lwjgl-opengl-native-\*.jar添加到classpath。这是工作。

    【讨论】:

      猜你喜欢
      • 2015-12-27
      • 2015-07-02
      • 1970-01-01
      • 2017-07-10
      • 2021-05-09
      • 2014-07-22
      • 2022-09-25
      • 2019-06-17
      • 2014-01-22
      相关资源
      最近更新 更多