2

I am using Sigar library to find system level resource usage in my java application. I added Sigar to my build.gradle (as suggested at http://mvnrepository.com/artifact/org.fusesource/sigar/1.6.4) as shown below:

// Apply the java plugin to add support for Java
apply plugin: 'java'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    jcenter()
    mavenCentral()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.fusesource:sigar:1.6.4'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

When I try to run my project I get following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Mem.gather(Lorg/hyperic/sigar/Sigar;)V
    at org.hyperic.sigar.Mem.gather(Native Method)
    at org.hyperic.sigar.Mem.fetch(Mem.java:30)
    at org.hyperic.sigar.Sigar.getMem(Sigar.java:304)
    at SystemResources.SystemMemory.getSystemMemory(SystemMemory.java:37)
    at SystemResources.SystemResources.getSystemResources(SystemResources.java:25)
    at Test.Driver.main(Driver.java:31)

After searching on Stackoverflow and other places, I know there are .dll and .so files whose path has to be modified. But when I add my library using gradle I am not able to change the path of my native library (in Properties->Java Build path->Library-> -> Native Library Path). Also i tried using

-Djava.library.path="./sigar-bin/lib" ${build_files} 

and

-Djava.library.path="./lib" ${build_files} 

in my environment variable in run configuration but it doesn't help.

I need to use gradle file to import sigar and that works perfectly. But how do I reference those .dll and .so files for my project to work?

After @Brian's comments this is what I am trying for build.gradle:

// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'eclipse'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'jcenter' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    maven {
      url "https://plugins.gradle.org/m2/"
    }
    jcenter()
    mavenCentral()
}
//Setting extra information for the project in key value pair
//Key - nativeLibsDir, Value - $buildDir/libs/natives
project.ext.set('nativeLibsDir', "${buildDir}/libs/natives")

configurations {
    nativeBundle
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // The production code uses the SLF4J logging API at compile time
    compile 'org.slf4j:slf4j-api:1.7.13'
    compile 'org.fusesource:sigar:1.6.4'
    runtime 'org.fusesource:sigar:1.6.4'
    nativeBundle 'org.fusesource:sigar:1.6.4:native'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testCompile 'junit:junit:4.12'
}

jar {
    manifest {
        attributes 'Main-Class': 'Test.Driver'
    }
    //baseName = project.name + '-all'
    //from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    //with jar
}

task extractNativeBundle(type: Sync) {
    from {
        configurations.nativeBundle.collect { zipTree(it) }
    }
    into file(project.nativeLibsDir)
}

test {
    dependsOn extractNativeBundle
    systemProperty "java.library.path", project.nativeLibsDir
}

Even after this I get following error:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/hyperic/sigar/SigarException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.hyperic.sigar.SigarException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more
DTCool
  • 33
  • 6
  • This might help http://stackoverflow.com/questions/29437888/using-gradle-with-native-dependencies – Brian Apr 05 '16 at 01:06
  • @BrianHart - I was going through the same post but I have some doubts. When I use the solution I get following error: Could not find property 'dist' on root project Also where to add the java.library.path. I tried changing the Native Library Path through Eclipse and method: tasks.withType(Test) { systemProperty "java.library.path", project.nativeLibsDir } in build.gradle, but it does not help. – DTCool Apr 05 '16 at 20:30

0 Answers0