How can I build and run my gradle project, when it

2019-03-04 14:04发布

问题:

I'm trying to use JInput for my project, and the library needs some binaries for my application to run.

I've discovered that, the required binaries are inside the library. But still, I get an UnsatisfiedLinkError when doing a gradle run because it cannot find the library binaries.

So I guess I need to unpack the library .jar before running the project? How do I use a library that has native binaries in it?

Here's my build.gradle

plugins {

    id "java"
    id "application"
}

repositories {

    mavenCentral()
}

dependencies {

    // https://mvnrepository.com/artifact/net.java.jinput/jinput
    compile group: 'net.java.jinput', name: 'jinput', version: '2.0.7'
}

EDIT: Here you can see the contents of the library

回答1:

Yes, there is an additional step. I followed these instructions: Getting started with JInput

The JInput dependencies you are linking in gradle file only set up the API for what methods/classes can be accessed. The actual machine code for those is inside the dynamic link libraries...DLL, i.e. the binaries.

First you download the DLLs:JInput DLL download into a folder.

Then you link the path to this folder with either a command line argument (-Djava.library.path= "C:/pathToLibrary" or dynamically by calling the following method before your class requiring it is created.

public static void setDllLibraryPath(String resourceStr) {
    try {
        System.setProperty("java.library.path", resourceStr);
        //System.setProperty("java.library.path", "/lib/x64");//for example

        Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
        fieldSysPath.setAccessible(true);
        fieldSysPath.set(null, null);//next time path is accessed, the new path will be imported
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}