1717import java .util .TimeZone ;
1818import java .util .regex .Matcher ;
1919import java .util .regex .Pattern ;
20+ import java .text .MessageFormat ;
2021
2122import static com .taosdata .jdbc .TSDBErrorNumbers .ERROR_INVALID_VARIABLE ;
2223
@@ -33,7 +34,50 @@ public class TSDBJNIConnector {
3334 private int affectedRows = -1 ;
3435
3536 static {
36- System .loadLibrary ("taos" );
37+ final String name = "TD_LIBRARY_PATH" ;
38+ final String taosLibName = "taos" ;
39+ final String fn = System .mapLibraryName (taosLibName );
40+ final String separator = System .getProperty ("path.separator" );
41+ String lp = System .getProperty (name ); // system property takes precedence over env
42+ if (lp == null ) {
43+ lp = System .getenv (name );
44+ }
45+ try {
46+ if (lp == null ) {
47+ // let system to choose how to search and load
48+ System .loadLibrary (taosLibName );
49+ } else {
50+ // we'll traverse the paths and try to load one after another until one succeeds
51+ final String [] paths = lp .split (separator );
52+
53+ boolean found = false ;
54+ for (final String path : paths ) {
55+ final String p = path + "/" + fn ;
56+ try {
57+ System .load (p );
58+ found = true ;
59+ } catch (UnsatisfiedLinkError e ) {
60+ } catch (Exception e ) {
61+ }
62+ if (found ) break ;
63+ }
64+ if (!found ) {
65+ final String pattern = "dynamic libraries `{0}` not found in: `{1}`" ;
66+ final String msg = MessageFormat .format (pattern , fn , lp );
67+ throw new RuntimeException (msg );
68+ }
69+ }
70+ } catch (UnsatisfiedLinkError e ) {
71+ final String pattern = "You can set the `{0}` through environment variables or " +
72+ "Java system properties to specify the search path for dynamic libraries `{1}`" ;
73+ final String msg = MessageFormat .format (pattern , name , fn );
74+ throw new RuntimeException (msg , e );
75+ } catch (Exception e ) {
76+ final String pattern = "You can set the `{0}` through environment variables or " +
77+ "Java system properties to specify the search path for dynamic libraries `{1}`" ;
78+ final String msg = MessageFormat .format (pattern , name , fn );
79+ throw new RuntimeException (msg , e );
80+ }
3781 }
3882
3983 /***********************************************************************/
0 commit comments