Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.35k stars 287 forks source link

Try loading library using System.loadLibrary first before trying other approaches #461

Closed mikehearn closed 1 year ago

mikehearn commented 2 years ago

When bundling jSerialComm with a JVM, it is often required to pre-extract the shared library so it can be signed, and to avoid littering the user's home directory. For this to work Java code has to try System.loadLibrary first before attempting other approaches like extracting from a JAR file. This will search the same location as used by the JVM for its other libraries, and some packaging tools like Conveyor set it to ensure that library copies that might be in c:\windows aren't picked up. Then the packaging process can sign the shared libraries, startup is nice and fast and everything will work fine.

hedgecrw commented 1 year ago

A couple of notes:

  1. This is already achievable by setting the Java Property jSerialComm.library.path with the expected directory containing the pre-extracted native library. (I assume packaging tools would be able to ensure that this property is set when starting up the application?)
  2. While it's feasible to issue a System.loadLibrary command before any other loading methods, this has been avoided thus far because it makes it way too easy for a user to inadvertently load a native library version that does not match the corresponding Java-side version of the library doing the loading (for instance, if a user updates the jSerialComm version in their application but doesn't manually update the corresponding native library in their system path). One solution would be to embed the library version in the filename itself so that only the correct/expected version gets loaded. I'm not opposed to making this change, but could you first let me know if Item 1 would be feasible instead?

Thanks!

mikehearn commented 1 year ago
  1. That can work yes.
  2. Do people have jSerialComm in their system paths, in practice?
hedgecrw commented 1 year ago

Haha, you'd be surprised what odd places people have ended up putting their native libraries!

Since this can already be achieved with jSerialComm.library.path, I'm not going to worry about this right now, but I'll add it as an enhancement for an upcoming major release.

hedgecrw commented 1 year ago

This has been implemented in the latest release v2.10.2

mikehearn commented 1 year ago

Thanks!