| 32 | } |
| 33 | |
| 34 | public static File GetFile(ClassLoader classLoader, String resourceFile) { |
| 35 | |
| 36 | try { |
| 37 | // need to decode the path, because for some reason, |
| 38 | // getResource().getFile replace space with %20 |
| 39 | // and it will cause File.exist() to fail |
| 40 | // what a mess |
| 41 | |
| 42 | // decoding the url seems to fix it |
| 43 | // https://stackoverflow.com/questions/31133361/how-to-get-file-from-resources-when-blank-space-is-in-path |
| 44 | // https://stackoverflow.com/a/12125969/14073678 |
| 45 | String filePath = URLDecoder.decode( |
| 46 | classLoader.getResource(resourceFile).getFile(), |
| 47 | "UTF-8"); |
| 48 | return new File(filePath); |
| 49 | } catch (UnsupportedEncodingException e) { |
| 50 | System.out.printf("Warning: cannot get file of resource file %s\n %s", resourceFile, e.getMessage()); |
| 51 | return new File(""); |
| 52 | } |
| 53 | } |
| 54 | } |