Including Hyperic Sigar library within jar while using sbt assembly for Scala project
Asked Answered
N

1

6

I'm building a Scala project with sbt and creating a fat jar with the sbt-assembly plugin. I'm able to add unmanaged jars (such as the Sigar jar) by adding the following to build.sbt.

unmanagedJars in Compile +=
    file("lib/hyperic-sigar-1.6.4/sigar-bin/lib/sigar.jar")

However, when I try running this, I get the following error because the *.so libraries are not included in the jar.

no libsigar-amd64-linux.so in java.library.path
org.hyperic.sigar.SigarException: no libsigar-amd64-linux.so in java.library.path
    at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
    at org.hyperic.sigar.Sigar.<clinit>(Sigar.java:100)


Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.ptql.SigarProcessQuery.create(Ljava/lang/String;)V
    at org.hyperic.sigar.ptql.SigarProcessQuery.create(Native Method)
    at org.hyperic.sigar.ptql.ProcessQueryFactory.getQuery(ProcessQueryFactory.java:66)
    at org.hyperic.sigar.ptql.ProcessFinder.findSingleProcess(ProcessFinder.java:44)

The libraries I want to include are in lib/hyperic-sigar-1.6.4/sigar-bin/lib/*.so and they need to be linked to a directory in the classpath within the jar. The only way I know of to do a mapping like this is:

resourceDirectory in Compile <<=
    baseDirectory{ _ / "lib/hyperic-sigar-1.6.4/sigar-bin/lib" }

This causes the *.so libraries to be added to root of the jar, but not a specific directory. How can I specify a resource map to map from lib/hyperic-sigar-1.6.4/sigar-bin/lib/*.so to a directory in the classpath in my jar? What is the terminology for what I'm trying to do?

Naif answered 19/2, 2013 at 20:8 Comment(2)
any particular reason why you're not using "org.fusesource" % "sigar" % "1.6.4" classifier("native") ?Speaker
@OlegYch, I changed to using libraryDependencies += "org.fusesource" % "sigar" % "1.6.4" in build.sbt, and I'm still getting the same error.Naif
S
4

Assuming that sigar is indeed capable of loading native libs from classpath, this should do the trick:

libraryDependencies += "org.fusesource" % "sigar" % "1.6.4" classifier("native") classifier("")

Otherwise, you need to unpack them from the jar manually and provide proper java.library.path

Speaker answered 22/3, 2013 at 2:34 Comment(1)
Will look into 'java.library.path' later. Thanks!Naif

© 2022 - 2024 — McMap. All rights reserved.