2

我正在使用 Sigar 库在我的 java 应用程序中查找系统级资源使用情况。我将 Sigar 添加到我的 build.gradle 中(如http://mvnrepository.com/artifact/org.fusesource/sigar/1.6.4所建议),如下所示:

// 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'
}

当我尝试运行我的项目时,出现以下错误:

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)

在 Stackoverflow 和其他地方搜索后,我知道有 .dll 和 .so 文件的路径必须修改。但是当我使用 gradle 添加我的库时,我无法更改本机库的路径(在 Properties->Java Build path->Library->-> Native Library Path 中)。我也尝试使用

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

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

在我的运行配置中的环境变量中,但它没有帮助。

我需要使用 gradle 文件来导入 sigar 并且效果很好。但是如何引用这些 .dll 和 .so 文件以使我的项目正常工作?

在@Brian 的评论之后,这就是我正在为 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
}

即使在此之后,我也会收到以下错误:

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
4

0 回答 0