diff --git a/netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java b/netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java index 44ec719a97..5f76264537 100644 --- a/netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java +++ b/netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java @@ -32,7 +32,10 @@ public class NetcdfClibrary { /** * Set the path and name of the netcdf c library. - * Must be called before load() is called. + *

+ * Must be called prior to calling {@link #isLibraryPresent() isLibraryPresent} + * or {@link #getForeignFunctionInterface() getForeignFunctionInterface}, as + * the C library can only be successfully loaded once. * * @param jna_path path to shared libraries, may be null. If null, will look for system property * "jna.library.path", then environment variable "JNA_PATH". If set, will set @@ -40,6 +43,12 @@ public class NetcdfClibrary { * @param lib_name library name, may be null. If null, will use "netcdf". */ public static void setLibraryNameAndPath(@Nullable String jna_path, @Nullable String lib_name) { + + if (nc4 != null) { + log.warn("netCDF-C library already set, ignoring."); + return; + } + lib_name = Strings.emptyToNull(lib_name); if (lib_name == null) { @@ -61,6 +70,11 @@ public static void setLibraryNameAndPath(@Nullable String jna_path, @Nullable St libName = lib_name; jnaPath = jna_path; + + if ((isClibraryPresent == null || !isClibraryPresent) && jnaPath != null) { + // call load to retry loading, but this time with jnaPath set + load(); + } } /** diff --git a/netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java b/netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java index 6c1893586d..ed85def1ae 100755 --- a/netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java +++ b/netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java @@ -74,8 +74,14 @@ public class Nc4Iosp extends AbstractIOServiceProvider implements IOServiceProvi private static final boolean transcodeStrings = Charset.defaultCharset() != StandardCharsets.UTF_8; /** - * set the path and name of the netcdf c library. - * must be called before load() is called. + * Set the path and name of the netcdf c library. + *

+ * This method will only work if the netCDF-C library was not found on the + * system path. If you need to use a specific version of the C library, and + * you have a different version on a system path, do not use this method. + * Instead, call {@link NetcdfClibrary#setLibraryNameAndPath(String, String) + * NetcdfClibrary.setLibraryNameAndPath} prior to any uses of + * the Nc4Iosp class. * * @param jnaPath path to shared libraries * @param libName library name @@ -83,7 +89,11 @@ public class Nc4Iosp extends AbstractIOServiceProvider implements IOServiceProvi */ @Deprecated public static void setLibraryAndPath(String jnaPath, String libName) { - NetcdfClibrary.setLibraryNameAndPath(jnaPath, libName); + if (nc4 != null) { + log.warn("Library already set, ignoring."); + } else { + NetcdfClibrary.setLibraryNameAndPath(jnaPath, libName); + } } /**