Class NativeLibrary

java.lang.Object
com.formdev.flatlaf.util.NativeLibrary

public class NativeLibrary extends Object
Helper class to load native library (.dll, .so or .dylib) stored in Jar.

Copies native library to users temporary folder before loading it.

Since:
1.1
  • Field Details

    • DELETE_SUFFIX

      private static final String DELETE_SUFFIX
      See Also:
    • deletedTemporary

      private static boolean deletedTemporary
    • loaded

      private final boolean loaded
  • Constructor Details

    • NativeLibrary

      public NativeLibrary(String libraryName, ClassLoader classLoader, boolean supported)
      Load native library from given classloader.

      Note regarding Java Platform Module System (JPMS): If classloader is null, the library can be only loaded from the module that contains this class. If classloader is not null, then the package that contains the library must be specified as "open" in module-info.java of the module that contains the library.

      Parameters:
      libraryName - resource name of the native library (without "lib" prefix and without extension)
      classLoader - the classloader used to locate the library, or null
      supported - whether the native library is supported on the current platform
    • NativeLibrary

      public NativeLibrary(File libraryFile, boolean supported)
      Load native library from given file.
      Parameters:
      libraryFile - the file of the native library
      supported - whether the native library is supported on the current platform
      Since:
      2
    • NativeLibrary

      public NativeLibrary(String libraryName, boolean supported)
      Load native library using System.loadLibrary(String). Searches for the library in classloader of caller (using ClassLoader.findLibrary(String)) and in paths specified in system properties sun.boot.library.path and java.library.path.
      Parameters:
      libraryName - name of the native library (without "lib" prefix and without extension)
      supported - whether the native library is supported on the current platform
      Since:
      2.6
  • Method Details

    • isLoaded

      public boolean isLoaded()
      Returns whether the native library is loaded.

      Returns false if not supported on current platform as specified in constructor or if loading failed.

    • loadLibraryFromJar

      private static boolean loadLibraryFromJar(String libraryName, ClassLoader classLoader)
    • loadLibraryFromFile

      private boolean loadLibraryFromFile(File libraryFile)
    • loadLibraryFromSystem

      private boolean loadLibraryFromSystem(String libraryName)
    • decorateLibraryName

      private static String decorateLibraryName(String libraryName)
      Add prefix and suffix to library name.
      • Windows: libraryName + ".dll"
      • macOS: "lib" + libraryName + ".dylib"
      • Linux: "lib" + libraryName + ".so"
    • createTempFile

      private static Path createTempFile(String libraryName) throws IOException
      Throws:
      IOException
    • getTempDir

      private static Path getTempDir() throws IOException
      Throws:
      IOException
    • deleteTemporaryFiles

      private static void deleteTemporaryFiles(Path tempDir)
    • deleteOrMarkForDeletion

      private static void deleteOrMarkForDeletion(File file)