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