Can't get LWJGL to run using IDEA and SBT

2019-04-29 06:04发布

问题:

I've been scratching myself in the head for a little over an hour with this, nothing on Google seems to be able to give me a decisive answer.

I'm using IntelliJ IDEA 13.1.3 with the scala and sbt plugins, Scala 2.11.1, and SBT 0.13

Thinking I was clever I added the Maven repository for LWJGL to my build.sbt

libraryDependencies += "org.lwjgl.lwjgl" % "lwjgl" % "2.9.1"

Only to later find out that I need to [point the compiler to the LWJGL natives].

Now here's the problem: Asking SBT to download libraries, doesn't put them in any of the project's directories, making the pointing to the libraries a tad difficult.

I tried using the [sbt-lwjgl-plugin] without any luck, even forcing an earlier version of SBT like the documentation suggests.

So I'm finding myself at an impasse, what am I supposed to do? Manually download the library and dump it into the project directories? Or is there a more automatic way for me to deal with this?

回答1:

You can include LWJGL (with natives) by simply adding the following snippet to your build.sbt:

libraryDependencies ++= {
  val version = "3.1.6"
  val os = "windows" // TODO: Change to "linux" or "macos" if necessary

  Seq(
    "lwjgl",
    "lwjgl-glfw",
    "lwjgl-opengl"
    // TODO: Add more modules here
  ).flatMap {
    module => {
      Seq(
        "org.lwjgl" % module % version,
        "org.lwjgl" % module % version classifier s"natives-$os"
      )
    }
  }
}

The classifier function sadly is very undocumented so it took me some time to find this out.



回答2:

I know this is quite a bit old, but I thought this might help others who come across this problem. What I did myself was download the jar file from the site, extract the natives from the jar and add them to my resources directory. As for lwjgl I added it to my sbt project as you have. During runtime, I extracted the natives from the jar and loaded the native libraries using

System.load("<native-library-name>")

then set the natives directory for lwjgl using

System.setProperty("org.lwjgl.librarypath", <natives-path>)

Also, as for extracting the natives from your jar file during runtime, you could do something like this

val source = Channels.newChannel(
NativesLoader.getClass.getClassLoader.getResourceAsStream("<native>"))

val fileOut = new File(<desination directory>, "<native path in jar>")
val dest = new FileOutputStream(fileOut)
dest.getChannel.transferFrom(source, 0, Long.MaxValue)
source.close()
dest.close()