使用Spring Boot加载本机库 [英] Loading native library with Spring Boot
问题描述
我有一个简单的Spring Boot项目,该项目加载本机库.问题是我不知道在运行应用程序时如何指定本机库的路径.
I have a simple Spring Boot project which loads native libraries. The problem is that I have no idea how to specify the path to the native library when running the application.
我读了很多帖子,解释了如何设置java.library.path
,但是每一个都会导致
I have read tons of posts explaining how to set java.library.path
but every single one leads to
java.lang.UnsatisfiedLinkError: /path/to/lib/libconnector.so: libconnector.so: cannot open shared object file: No such file or directory
如果我从命令行按顺序运行以下两个命令,则该项目有效:
The project works if I run these two commands in a sequence from command line:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/lib
./gradlew bootRun
库已加载并可以使用.但是我无法在我的gradle文件中指定库路径.我尝试过
The library is loaded and works. But I am unable to specify the library path in my gradle file. I tried
run {
systemProperty 'java.library.path', file('/path/to/lib')
}
bootRun {
systemProperty 'java.library.path', file('/path/to/lib')
}
及其各种变化.还尝试将VM参数添加到我的IDE等,但没有任何效果.有人可以解释我在做什么错吗?
and all sorts of variations of this. Also tried adding VM arguments to my IDE etc. but nothing works. Could someone explain what am I doing wrong?
这是我加载本机库(位于$projectRoot/lib
中)的方式:
This is how I load the native library (located in $projectRoot/lib
):
static {
// load connector library
File lib = new File("lib/" + System.mapLibraryName("connector"));
System.load(lib.getAbsolutePath());
}
推荐答案
我终于解决了我的问题.运行应用程序时,我应该将LD_LIBRARY_PATH
作为环境变量而不是java.library.path
作为系统属性传递.
I finally solved my problem. I should be passing LD_LIBRARY_PATH
as an environment variable instead of java.library.path
as system property when running the application.
以下Gradle修改解决了我的问题:
The following Gradle modification solved my issue:
tasks.withType(JavaExec) {
environment('LD_LIBRARY_PATH', 'lib')
}
这篇关于使用Spring Boot加载本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!