简体   繁体   中英

Workflow for native libraries in Java / Maven

I'm trying to work out if there is a definitive, recommended, robust way to work with native libraries in Java and Maven.

I know it's possible to do this by putting libraries in the right places, configuring java.library.path and calling System.loadLibrary etc. - but I want a proper solution that works across the whole development / deployment life-cycle and doesn't involve lots of hacks.

My requirements seem fairly straightforward:

  • Configuration 100% via Maven
  • Ability to include native dependencies for different platforms / architectures (mainly Linux and Windows, others would be nice though...)
  • Ability to create a runnable .jar that doesn't require the user to fiddle with command line options to run it (ie it will unpack and load any native dependencies it needs at runtime, preferably as temp files or something like that)
  • The code works during development (in Eclipse)
  • The code works during normal Maven builds at the command line

Basically, I want natives to just work both during development and for the user at runtime.

Is this possible? If so what do I need to configure to make it happen?

It certainly is possible. A good example that I came across which solves the same problem is sqlite wrapper at https://bitbucket.org/xerial/sqlite-jdbc/overview

By following a pre defined packaging structure for each architecture, we can programmatically extract the desired native library from class path into a temp folder and can set the desired java.library.path to load the native library.

  • All native libraries would be packaged in the src/main/resources folder
  • At runtime there needs to be a bootstrap code which can determine the OS, architecture and choose the right native library to extract
  • the same bootstrap code can extract the native library into a temp folder and call System.loadLibrary

I have not personally tried executing the above flow but could not think of any other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM