当需要二进制库文件(JInput)时,如何构建和运行gradle项目? [英] How can I build and run my gradle project, when it needs binary library files (JInput)?

查看:169
本文介绍了当需要二进制库文件(JInput)时,如何构建和运行gradle项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目使用 JInput ,并且该库需要一些二进制文件应用程序运行.

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

我发现,所需的二进制文件在库中.但是,仍然可以在执行gradle run时得到一个UnsatisfiedLinkError,因为它找不到库二进制文件.

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.

所以我想我需要在运行项目之前打开库.jar的包装?如何使用其中包含本机二进制文件的库?

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?

这是我的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'
}

在这里您可以看到库的内容

Here you can see the contents of the library

推荐答案

是的,还有一个附加步骤.我遵循以下指示: JInput入门

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

您在gradle文件中链接的JInput依赖项仅针对可以访问哪些方法/类设置API.这些的实际机器代码在动态链接库... DLL(即二进制文件)中.

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.

首先,您下载DLL: JInput DLL下载到一个文件夹.

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

然后,使用命令行参数(-Djava.library.path= "C:/pathToLibrary")或通过在创建需要它的类之前调用​​以下方法来动态链接此文件夹的路径.

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);
    }
}

这篇关于当需要二进制库文件(JInput)时,如何构建和运行gradle项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆