Program and section headers removed from shared library in resource folder

Hi there,
I want to include a native library, in my final jar. I’m doing this by putting the file in src/main/resources/ folder, Gradle will include it just fine. However it seems Gradle does something to the file? The output before and after a gradle build is different.


target/release/ ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=3e7ef68e42de41612735b9d19e04fb24ccf926f0, with debug_info, not stripped


server/plugins/ ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), no program header, missing section headers

This causes a UnsatisfiedLinkError to be thrown when I try to load the library in Java: ELF file's phentsize not the expected size.

What could be causing this, and how do I solve it?

Using Gradle 6.5.1, on Ubuntu 20.04 LTS

I remember a similar issue with binary files that would be modified as they were “text processed” before. Is there any particular configuration to the ProcessResources task? We would be mostly looking at any configuration that would edit a file content.

Alternatively, you can add your binary directly to the Jar task. One downside of such approach is the native library won’t be passed on to the consumer if you are using the java-library plugin thanks to the optimization within a multi-project build.

Finally, you can look at using the Nokee plugins for JNI library: Nokee Samples

Thanks for your response!
I’m using a pretty much stock build.gradle file. I dont think I’ve touched the ProcessResources Task at all.

I’m not sure what you ment with you can add your binary directly to the Jar task., But I’m guessing it means letting Gradle compile the native project, rather than doing it externally. I’m using Rust for the native code, which Gradle doesn’t support as far as I know. Same applies to Nokee.

I’ve got a working solution though, it might be a bit ghetto but, a Copy task works:

task copyNativeBinaries(type: Copy) {
	from "$rootDir/librconsole/target/x86_64-unknown-linux-gnu/release/"
	into "$buildDir/resources/main/x86_64/linux/"
	from "$rootDir/librconsole/target/x86_64-pc-windows-gnu/release/librconsole.dll"
	into "$buildDir/resources/main/x86_64/windows/"

I’m then using a Makefile to build both the .so and the .dll.
It’s a bit of a ghetto solution, and I should probably be able to just stash the file in src/main/resources without Gradle corrupting the file, but for now this works.

Thanks for your help!