0

I am writing a Java wrapper for a C-Library that I own. I build with CMake.

Here is my CMakeLists.txt file :

cmake_minimum_required(VERSION 3.4)
project(MyLib-java)
    
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)

# JNI wrapper
add_library(mylib-java SHARED mylib-java.cpp)
target_link_libraries(mylib-java PRIVATE ${JNI_LIBRARIES})
target_include_directories(mylib-java PUBLIC ${JNI_INCLUDE_DIRS})

# JAR file
add_jar(
  mylib-java
  SOURCES
    MyLib.java
  ENTRY_POINT
    lib.my.Main
)

It works well, the .so file is built, .jar file is created and populated with .class files.

My problem is that I don't know how to put the .so file in the .jar file. Please help !

Thanks in advance

  • Why do you want to add a native binary (the *.so will be most likely an elf file) into a jar for the Java Virtual machine? Frankly, it is a while since I did Java, but the Java VM searches for the shared libs at java.library.path / LD_LIBRARY_PATH and (AFAIK) not in a JAR. Ask yourself: how should the native loader access the jar and extract the so (or DLL) out of it? Maybe this [answer](https://stackoverflow.com/questions/51098258/jni-cant-find-shared-library-after-program-has-been-deployed) provides what you need. I think you have to deploy Java and native parts separately. – Sonic78 Apr 14 '22 at 14:38
  • @Sonic78 Wether it's a good idea or not may be debatable, but it's certainly feasible, as per https://stackoverflow.com/questions/2937406/how-to-bundle-a-native-library-and-a-jni-library-inside-a-jar –  Apr 14 '22 at 14:41
  • Take a look here: https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo035 – Oo.oO Apr 16 '22 at 09:08

0 Answers0