Skip to content

Commit 09b8ecd

Browse files
ClemDoumAngledLuffa
authored andcommitted
fix(io): use Thread.currentThread().getContextClassLoader() instead of system class loader when loading resources
1 parent adb8b7a commit 09b8ecd

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/edu/stanford/nlp/io/IOUtils.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,33 +405,36 @@ public static ObjectInputStream readStreamFromString(String filenameOrUrl)
405405

406406
/**
407407
* Locates a file in the CLASSPATH if it exists. Checks both the
408-
* System classloader and the IOUtils classloader, since we had
408+
* current thread context classloader which default to the system class classloader unless the user sets another
409+
* classloader explicitly, then we fallaback to the IOUtils classloader, since we had
409410
* separate users asking for both of those changes.
410411
*/
411412
private static InputStream findStreamInClassLoader(String name) {
412-
InputStream is = ClassLoader.getSystemResourceAsStream(name);
413+
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
414+
InputStream is = contextClassLoader.getResourceAsStream(name);
413415
if (is != null)
414416
return is;
415417

416418
// windows File.separator is \, but getting resources only works with /
417-
is = ClassLoader.getSystemResourceAsStream(name.replaceAll("\\\\", "/"));
419+
is = contextClassLoader.getResourceAsStream(name.replaceAll("\\\\", "/"));
418420
if (is != null)
419421
return is;
420422

421423
// Classpath doesn't like double slashes (e.g., /home/user//foo.txt)
422-
is = ClassLoader.getSystemResourceAsStream(name.replaceAll("\\\\", "/").replaceAll("/+", "/"));
424+
is = contextClassLoader.getResourceAsStream(name.replaceAll("\\\\", "/").replaceAll("/+", "/"));
423425
if (is != null)
424426
return is;
425427

426-
is = IOUtils.class.getClassLoader().getResourceAsStream(name);
428+
ClassLoader fallbackClassLoader = IOUtils.class.getClassLoader();
429+
is = fallbackClassLoader.getResourceAsStream(name);
427430
if (is != null)
428431
return is;
429432

430-
is = IOUtils.class.getClassLoader().getResourceAsStream(name.replaceAll("\\\\", "/"));
433+
is = fallbackClassLoader.getResourceAsStream(name.replaceAll("\\\\", "/"));
431434
if (is != null)
432435
return is;
433436

434-
is = IOUtils.class.getClassLoader().getResourceAsStream(name.replaceAll("\\\\", "/").replaceAll("/+", "/"));
437+
is = fallbackClassLoader.getResourceAsStream(name.replaceAll("\\\\", "/").replaceAll("/+", "/"));
435438
// at this point we've tried everything
436439
return is;
437440
}

0 commit comments

Comments
 (0)