Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

spring boot - Include and use native lib files (.so, .dylib) in "executable JAR"

I have a multimodule Spring Boot project with Maven. I am using spring-boot-maven-plugin to package this application into an executable JAR. This JAR is deployed to PCF (Pivotal Cloud Foundry).

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>my.AppMain</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Now I would like to use PDFTron library - they provided me with a JAR ,.so and .dylib files (.dll too but I dont need those). They said that the JAR has to be in the same directory as the so and dylib files in order to work properly.

I tried to use this library in the project but I cant make it work. I am running out of ideas, I believe I have read almost all possible posts to this topic.

I have tried:

  1. load each of this files to maven repo, include each one as (dylib, so and pdfnet.jar), but it resulted in error message: Caused by: java.lang.UnsatisfiedLinkError: no PDFNetC in java.library.path:

  2. put all the files (dylib, so and pdfnet.jar) into the src/main/resources, include just provided PDFNet.jar as but it resulted in error message: Caused by: java.lang.UnsatisfiedLinkError: no PDFNetC in java.library.path:

  3. Only way that worked was when I used maven-dependency-plugin to unpack the PDFTron dependencies into the target directory and update the -Djava.library.path to point at this created directory - but this can be done only at my local machine, because PDFTron dependencies were unpacked at "package" phase. To the PCF we are depolying only the executable JAR, so there is no target directory at PCF, so this was not an "production ready" option.

My question is, how can I include these files into this "executable JAR" so it will work properly ? Is there a way to unpack them at runtime ? Or do I have to use other plugin than spring-boot-maven-plugin ?

I can modify the -Djava.library.path at PCF, but I would like to avoid deploying another zip/jar/folder along with this JAR (basically I want to avoid changing the CI/CD deploy script as it is not under my control - and right now we are deploying only the executable JAR)

I am using java version "11.0.8" and spring-boot version "2.2.8.RELEASE"

question from:https://stackoverflow.com/questions/65935083/include-and-use-native-lib-files-so-dylib-in-executable-jar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You only need two files, per-platform.

  • PDFNet.jar is for all platforms.
  • libPDFNetC.so for Linux
  • PDFNetC.dll for Windows
  • PDFNetC.dylib for macOS

You can see in the samples, there are RuntTest.[sh|exe] files that show the settings that you can use. For example

#!/bin/sh
TEST_NAME=AddImageTest
javac -cp ../../../Lib/PDFNet.jar *.java
java -Djava.library.path=../../../Lib -cp .:../../../Lib/PDFNet.jar $TEST_NAME

So -Djava.library.path= points to the folder containing libPDFNetC.so and/or PDFNetC.dll.

It sounds like you have deployment specific issue with your tech stack, but I assume there is a way, same as how you would deploy any images/fonts or other resources. Though maybe your tech stack puts some restrictions on where java.library.path can point to, but that should be in the documentation supplied by what you are using.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...