Skip to content

Commit 94e5e2c

Browse files
committed
Allow C library to be loaded after Nc4Iosp initialization for some cases
If the netCDF-C library was not found on the system library path upon initialization of the Nc4Iosp class, retry loading when calling Nc4Iosp.setLibraryAndPath. Clarify the javadocs for the setLibraryNameAndPath methods of the Nc4Iosp and NetcdfClibrary classes. Addresses #1469
1 parent e89a2bb commit 94e5e2c

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

netcdf4/src/main/java/ucar/nc2/ffi/netcdf/NetcdfClibrary.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,23 @@ public class NetcdfClibrary {
3232

3333
/**
3434
* Set the path and name of the netcdf c library.
35-
* Must be called before load() is called.
35+
* <p>
36+
* Must be called prior to calling {@link #isLibraryPresent() isLibraryPresent}
37+
* or {@link #getForeignFunctionInterface() getForeignFunctionInterface}, as
38+
* the C library can only be successfully loaded once.
3639
*
3740
* @param jna_path path to shared libraries, may be null. If null, will look for system property
3841
* "jna.library.path", then environment variable "JNA_PATH". If set, will set
3942
* the environment variable "JNA_PATH".
4043
* @param lib_name library name, may be null. If null, will use "netcdf".
4144
*/
4245
public static void setLibraryNameAndPath(@Nullable String jna_path, @Nullable String lib_name) {
46+
47+
if (nc4 != null) {
48+
log.warn("netCDF-C library already set, ignoring.");
49+
return;
50+
}
51+
4352
lib_name = Strings.emptyToNull(lib_name);
4453

4554
if (lib_name == null) {
@@ -61,6 +70,11 @@ public static void setLibraryNameAndPath(@Nullable String jna_path, @Nullable St
6170

6271
libName = lib_name;
6372
jnaPath = jna_path;
73+
74+
if ((isClibraryPresent == null || !isClibraryPresent) && jnaPath != null) {
75+
// call load to retry loading, but this time with jnaPath set
76+
load();
77+
}
6478
}
6579

6680
/**

netcdf4/src/main/java/ucar/nc2/jni/netcdf/Nc4Iosp.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,26 @@ public class Nc4Iosp extends AbstractIOServiceProvider implements IOServiceProvi
7474
private static final boolean transcodeStrings = Charset.defaultCharset() != StandardCharsets.UTF_8;
7575

7676
/**
77-
* set the path and name of the netcdf c library.
78-
* must be called before load() is called.
77+
* Set the path and name of the netcdf c library.
78+
* <p>
79+
* This method will only work if the netCDF-C library was not found on the
80+
* system path. If you need to use a specific version of the C library, and
81+
* you have a different version on a system path, do not use this method.
82+
* Instead, call {@link NetcdfClibrary#setLibraryNameAndPath(String, String)
83+
* NetcdfClibrary.setLibraryNameAndPath} prior to any uses of
84+
* the Nc4Iosp class.
7985
*
8086
* @param jnaPath path to shared libraries
8187
* @param libName library name
8288
* @deprecated use NetcdfClibrary.setLibraryNameAndPath
8389
*/
8490
@Deprecated
8591
public static void setLibraryAndPath(String jnaPath, String libName) {
86-
NetcdfClibrary.setLibraryNameAndPath(jnaPath, libName);
92+
if (nc4 != null) {
93+
log.warn("Library already set, ignoring.");
94+
} else {
95+
NetcdfClibrary.setLibraryNameAndPath(jnaPath, libName);
96+
}
8797
}
8898

8999
/**

0 commit comments

Comments
 (0)