@@ -69,7 +69,6 @@ public static synchronized void loadLibrary(String libname, List<String> depende
69
69
70
70
private static boolean verbose = false ;
71
71
72
- private static boolean usingModules = false ;
73
72
private static File libDir = null ;
74
73
private static String libPrefix = "" ;
75
74
private static String libSuffix = "" ;
@@ -116,8 +115,9 @@ private static String[] initializePath(String propname) {
116
115
117
116
private static void loadLibraryInternal (String libraryName , List <String > dependencies , Class caller ) {
118
117
// The search order for native library loading is:
119
- // - try to load the native library from the same folder as this jar
120
- // (only on non-modular builds)
118
+ // - try to load the native library from either ${java.home}
119
+ // (for jlinked javafx modules) or from the same folder as
120
+ // this jar (if using modular jars)
121
121
// - if the native library comes bundled as a resource it is extracted
122
122
// and loaded
123
123
// - the java.library.path is searched for the library in definition
@@ -130,7 +130,7 @@ private static void loadLibraryInternal(String libraryName, List<String> depende
130
130
// since it isn't applicable to Jigsaw.
131
131
loadLibraryFullPath (libraryName );
132
132
} catch (UnsatisfiedLinkError ex ) {
133
- if (verbose && ! usingModules ) {
133
+ if (verbose ) {
134
134
System .err .println ("WARNING: " + ex );
135
135
}
136
136
@@ -326,51 +326,73 @@ static byte[] calculateCheckSum(File file) {
326
326
}
327
327
328
328
329
+ private static File libDirForJRT () {
330
+ String javaHome = System .getProperty ("java.home" );
331
+
332
+ if (javaHome == null || javaHome .isEmpty ()) {
333
+ throw new UnsatisfiedLinkError ("Cannot find java.home" );
334
+ }
335
+
336
+ // Set the native directory based on the OS
337
+ String osName = System .getProperty ("os.name" );
338
+ String relativeDir = null ;
339
+ if (osName .startsWith ("Windows" )) {
340
+ relativeDir = "bin/javafx" ;
341
+ } else if (osName .startsWith ("Mac" )) {
342
+ relativeDir = "lib" ;
343
+ } else if (osName .startsWith ("Linux" )) {
344
+ relativeDir = "lib" ;
345
+ }
346
+
347
+ // Location of native libraries relative to java.home
348
+ return new File (javaHome + "/" + relativeDir );
349
+ }
350
+
351
+ private static File libDirForJarFile (String classUrlString ) throws Exception {
352
+ // Strip out the "jar:" and everything after and including the "!"
353
+ String tmpStr = classUrlString .substring (4 , classUrlString .lastIndexOf ('!' ));
354
+ // Strip everything after the last "/" or "\" to get rid of the jar filename
355
+ int lastIndexOfSlash = Math .max (tmpStr .lastIndexOf ('/' ), tmpStr .lastIndexOf ('\\' ));
356
+
357
+ // Set the native directory based on the OS
358
+ String osName = System .getProperty ("os.name" );
359
+ String relativeDir = null ;
360
+ if (osName .startsWith ("Windows" )) {
361
+ relativeDir = "../bin" ;
362
+ } else if (osName .startsWith ("Mac" )) {
363
+ relativeDir = "." ;
364
+ } else if (osName .startsWith ("Linux" )) {
365
+ relativeDir = "." ;
366
+ }
367
+
368
+ // Location of native libraries relative to jar file
369
+ String libDirUrlString = tmpStr .substring (0 , lastIndexOfSlash )
370
+ + "/" + relativeDir ;
371
+ return new File (new URI (libDirUrlString ).getPath ());
372
+ }
373
+
329
374
/**
330
- * Load the native library from the same directory as the jar file
331
- * containing this class.
375
+ * Load the native library either from the same directory as the jar file
376
+ * containing this class, or from the Java runtime .
332
377
*/
333
378
private static void loadLibraryFullPath (String libraryName ) {
334
379
try {
335
- if (usingModules ) {
336
- throw new UnsatisfiedLinkError ("ignored" );
337
- }
338
380
if (libDir == null ) {
339
381
// Get the URL for this class, if it is a jar URL, then get the
340
382
// filename associated with it.
341
383
String theClassFile = "NativeLibLoader.class" ;
342
384
Class theClass = NativeLibLoader .class ;
343
385
String classUrlString = theClass .getResource (theClassFile ).toString ();
344
386
if (classUrlString .startsWith ("jrt:" )) {
345
- // Suppress warning messages
346
- usingModules = true ;
347
- throw new UnsatisfiedLinkError ("ignored" );
348
- }
349
- if (!classUrlString .startsWith ("jar:file:" ) || classUrlString .indexOf ('!' ) == -1 ) {
387
+ libDir = libDirForJRT ();
388
+ } else if (classUrlString .startsWith ("jar:file:" ) && classUrlString .indexOf ('!' ) > 0 ) {
389
+ libDir = libDirForJarFile (classUrlString );
390
+ } else {
350
391
throw new UnsatisfiedLinkError ("Invalid URL for class: " + classUrlString );
351
392
}
352
- // Strip out the "jar:" and everything after and including the "!"
353
- String tmpStr = classUrlString .substring (4 , classUrlString .lastIndexOf ('!' ));
354
- // Strip everything after the last "/" or "\" to get rid of the jar filename
355
- int lastIndexOfSlash = Math .max (tmpStr .lastIndexOf ('/' ), tmpStr .lastIndexOf ('\\' ));
356
-
357
- // Set the native directory based on the OS
358
- String osName = System .getProperty ("os.name" );
359
- String relativeDir = null ;
360
- if (osName .startsWith ("Windows" )) {
361
- relativeDir = "../bin" ;
362
- } else if (osName .startsWith ("Mac" )) {
363
- relativeDir = "." ;
364
- } else if (osName .startsWith ("Linux" )) {
365
- relativeDir = "." ;
366
- }
367
-
368
- // Location of native libraries relative to jar file
369
- String libDirUrlString = tmpStr .substring (0 , lastIndexOfSlash )
370
- + "/" + relativeDir ;
371
- libDir = new File (new URI (libDirUrlString ).getPath ());
372
393
373
394
// Set the lib prefix and suffix based on the OS
395
+ String osName = System .getProperty ("os.name" );
374
396
if (osName .startsWith ("Windows" )) {
375
397
libPrefix = "" ;
376
398
libSuffix = ".dll" ;
0 commit comments