GoatPonny
GoatPonny

Reputation: 87

Failed to load library liblwjgl.so

I was trying to export .jar file of my lwjgl game so I can send it to my friend using gradle. When I've opened jar in /build/libs via typing java -jar [name of file] this error message appear:

[LWJGL] Failed to load a library. Possible solutions:
    a) Add the directory that contains the shared library to -Djava.library.path or -Dorg.lwjgl.librarypath.
    b) Add the JAR that contains the shared library to the classpath.
[LWJGL] Enable debug mode with -Dorg.lwjgl.util.Debug=true for better diagnostics.
[LWJGL] Enable the SharedLibraryLoader debug mode with -Dorg.lwjgl.util.DebugLoader=true for better diagnostics.
Exception in thread "main" java.lang.UnsatisfiedLinkError: Failed to locate library: liblwjgl.so
    at org.lwjgl.system.Library.loadSystem(Library.java:164)
    at org.lwjgl.system.Library.loadSystem(Library.java:63)
    at org.lwjgl.system.Library.<clinit>(Library.java:51)
    at org.lwjgl.system.MemoryUtil.<clinit>(MemoryUtil.java:100)
    at org.lwjgl.system.Pointer$Default.<clinit>(Pointer.java:67)
    at org.lwjgl.system.Callback.<clinit>(Callback.java:40)
    at org.example.app.Engine.init(Engine.kt:64)
    at org.example.app.Engine.run(Engine.kt:53)
    at org.example.app.ApplicationKt.main(Application.kt:4)
    at org.example.app.ApplicationKt.main(Application.kt)

I know that this kinda says what I need to do, but I don't really understand where should I add for example "-Djava.library.path".

here's my build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.5.30'
}

group 'org.example'
version '1.0-SNAPSHOT'

project.ext.lwjglVersion = "3.3.0"
project.ext.lwjglNatives = "natives-linux"

repositories {
    mavenCentral()
}

dependencies {
    implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")

    implementation 'org.joml:joml:1.10.3'
    implementation "org.lwjgl:lwjgl"
    implementation "org.lwjgl:lwjgl-assimp"
    implementation "org.lwjgl:lwjgl-glfw"
    implementation "org.lwjgl:lwjgl-openal"
    implementation "org.lwjgl:lwjgl-opengl"
    implementation "org.lwjgl:lwjgl-stb"
    runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-assimp::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-opengl::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-stb::$lwjglNatives"
}

jar {
    duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
    manifest {
        attributes 'Main-Class': 'org.example.app.ApplicationKt'
    } from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

sourceSets {
    main {
        resources {
            srcDir 'src/../lib'
        }
    }
}

gradle.properties:

kotlin.code.style=official
org.gradle.jvmargs=-Dkotlin.daemon.jvm.options=--illegal-access=permit

It works perfectly fine in IntelliJ, it gives an error only when trying to open outside of the IDE.

If it matters I'm using linux (POP_OS) and I'm writing this game in kotlin.

Upvotes: 2

Views: 1774

Answers (1)

httpdigest
httpdigest

Reputation: 5797

You are currently not including the runtime natives jar files of LWJGL into your fat jar file.

So, you don't need to add any JVM arguments like -Djava.library.path or -Dorg.lwjgl.librarypath but merely must make sure that the "natives-linux" dependencies/jar files are in the classpath when you run the jar.

One way to achieve this in your case is by not only iterating over the compile-time dependencies but over the runtime dependencies in your jar task to also collect those and add them to the fat jar file that you build, because you definitely will need them at runtime.

So, change this line:

} from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

to:

} from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }

LWJGL 3 itself will then, at runtime, scan the classpath and detect the shared libraries files in the jar files, extract and load them appropriately.

Upvotes: 3

Related Questions