Skip to content

load H5's JNI library from the main code - #13

Open
bogovicj wants to merge 1 commit into
JaneliaSciComp:windows-test-fixesfrom
bogovicj:load-library
Open

bogovicj wants to merge 1 commit into
JaneliaSciComp:windows-test-fixesfrom
bogovicj:load-library

Conversation

@bogovicj

@bogovicj bogovicj commented Oct 6, 2026

Copy link
Copy Markdown

This or something like it will be needed in updating n5-hdf5.
(with help from Claude)

* Add a static HDF5NativeLibrary.load() method

@mkitti mkitti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, moving the loading into the main code is the right fix: downstream code had no way to load hdf5_java itself. I built n5-hdf5 saalfeldlab/n5-hdf5#50 against this branch, and it passes with no workaround (47 tests, 5 skipped, same as its master).

Blocking: class-initialization order. Five public enums initialize their constants from HDF5Constants:

  • IHDF5WriterConfigurator.FileFormatVersion
  • HDF5DataClass
  • HDF5StorageLayout
  • HDF5EnumerationType.EnumStorageForm
  • CharacterEncoding

If any of them, or anything using them, is touched before HDF5Factory/HDF5FactoryProvider/HDFHelper, HDF5Constants runs H5.loadH5Lib() before load(). H5's class initialization then fails, and since that's permanent in Java, every later HDF5 call fails too (UnsatisfiedLinkError: 'int hdf.hdf5lib.H5.H5dont_atexit()').

  • CI: that's the 306 macOS failures here. HDF5ArchiverTest touches FileFormatVersionBounds first, and Linux passes only because of its test order. The removed NativeLibraryBootstrap used to hide it.
  • Downstream: reading CharacterEncoding.UTF8 before HDF5Factory.open(...) fails the same way.
  • Scope: initializing each class of the jar first in a fresh JVM, 15 of 1,104 classes break HDF5 this way.

Smaller:

  • loaded = true is set before loading succeeds, so after one failure later callers skip load() silently and only see the error above.
  • System.setProperty(..., Loader.load(...)) throws if Loader.load() returns null, which it does when JavaCPP's library loading is disabled.

I've put fixes for all three in #14, stacked on this PR: enum constants read HDF5 values through HDF5NativeLibrary.intConstant(() -> ...)/longConstant(...), which call load() first. With it, 0 of 1,104 classes fail. #14's CI passes on macOS and Linux (Java 8 and 21) without the bootstrap; Windows still fails only with the separate-HDF5-libraries error that bytedeco/javacpp-presets#1814 fixes.

Compatibility with javacpp-presets #1814: once bytedeco/javacpp-presets#1814 is in, hdf5_java's own static initializer also sets hdf.hdf5lib.H5.hdf5lib, to the same path. This PR is still needed after that, because something has to load hdf5_java first. The extra setProperty is just redundant.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants