Вопрос по – Интеграция собственных системных библиотек с SBT

31

Что является хорошим способом интеграции различных задач SBT с собственными библиотеками (например, изJOGL, LWGL, или жеJCuda? В частности,

Is there a recommended way to include a native library in the run task? A discussion on the SBT mailing list suggests these possibilities:

Modify JavaOptions to include -Djava.library.path=<path to native libraries>, and then modify the run task to fork the JVM. (See this plugin for an example.) Use the SBT initialize setting to run code that calls System.setProperty(...) to configure java.library.path. Again, run must fork. Put the native libraries on the classpath before launching SBT.

The last one has the advantage that run need not fork, but the disadvantage that the configuration must be done outside SBT.

Can I automatically include native libraries in the Eclipse project generated by the sbteclipse plugin? It's possible to rewrite the .project file in a post-processing step. Is there example code? Is there a better way?

Can native libraries be included in the runnable Jar that is generated by a plugin such as sbt-assembly, sbt-onejar or sbt-proguard?

Я предполагаю, что нет прямой настройки SBT для нативных библиотек. Если бы что-то подобное существовало, могли ли вышеуказанные задачи прозрачно обрабатывать собственные библиотеки?

Ваш Ответ

3   ответа
0

/lib/*.jnilibsbt test.

[error] java.lang.UnsatisfiedLinkError: Fatal execution error, caused by no jniortools in java.library.path

System.loadLibrary("jniortools").

new File("lib").listFiles().map(_.getAbsolutePath).filter(_.endsWith("jniortools.jnilib")).foreach(System.load)
0

lib_extra

add jna to libraryDependencies:

libraryDependencies ++= Seq("net.java.dev.jna" % "jna-platform" % "4.1.0")

add these code to build.sbt:

unmanagedResourceDirectories in Compile += baseDirectory.value / "lib_extra"

includeFilter in (Compile, unmanagedResourceDirectories):= ".dll,.so"

28

java.library.pathSystem.loadLibrarySystem.load

java.library.path

System.load

Create an SBT task that compiles your native sources (with javah and gcc), takes the resulting .so files and any .so files it depends on, puts them in a jar (as resources) in the target directory, and adds the path to the jar to unmanagedJars in Compile. Create a Scala method to load a library. Instead of calling System.loadLibrary, it will use Class.getResourceAsStream to read the library, File.createTempFile to write it somewhere on the filesystem, and System.load to load it into the JVM. Now instead of calling System.loadLibrary as you would before, call MyClasspathJniLoader.loadLibrary.

java.library.pathSystem.loadLibrary

Use a similar SBT task to put all of the libraries on their native path into a jar as resources, and put that jar on the clsaspath. Create a Scala method that takes a function and a list of library names, creates a temp directory, reads those libraries from the jar's resources into files in the temp directory, adds the temp directory to java.library.path, calls the passed in function, and finally reverts the java.library.path back to what it was before. The first time you call into the native library (when it presumably will statically initialize and make the System.loadLibrary call), wrap that particular call in your method with the list of libraries it will load. That way, when it calls System.loadLibrary, all of its libraries will be on java.library.path and will be loaded successfully.

java.library.path

Похожие вопросы