| 60 | } |
| 61 | |
| 62 | private static void try_load_from_jar() throws IOException { |
| 63 | String nativesPrefix = "natives/" + PLATFORM_DIR + "/"; |
| 64 | |
| 65 | // create temp directory |
| 66 | Path tempDirectory = Files.createTempDirectory("tmplibvw"); |
| 67 | tempDirectory.toFile().deleteOnExit(); |
| 68 | |
| 69 | // Extract library and dependencies |
| 70 | File jarFile = new File(Native.class.getProtectionDomain().getCodeSource().getLocation().getPath()); |
| 71 | JarFile jar = null; |
| 72 | boolean foundLibrary = false; |
| 73 | try { |
| 74 | jar = new JarFile(jarFile); |
| 75 | Enumeration<JarEntry> entries = jar.entries(); |
| 76 | while (entries.hasMoreElements()) { |
| 77 | final String name = entries.nextElement().getName(); |
| 78 | if (name.endsWith("/")) |
| 79 | continue; |
| 80 | |
| 81 | if (name.startsWith(nativesPrefix)) { |
| 82 | Path dest_file = tempDirectory.resolve(name); |
| 83 | if (!dest_file.normalize().startsWith(tempDirectory)) { |
| 84 | throw new RuntimeException("Bad zip entry"); |
| 85 | } |
| 86 | Path parent = dest_file.getParent(); |
| 87 | if (parent != null) |
| 88 | Files.createDirectories(parent); |
| 89 | |
| 90 | Files.copy(Native.class.getResourceAsStream("/" + name), dest_file); |
| 91 | foundLibrary = true; |
| 92 | } |
| 93 | } |
| 94 | } finally { |
| 95 | if (jar != null) |
| 96 | jar.close(); |
| 97 | } |
| 98 | |
| 99 | if (!foundLibrary) { |
| 100 | throw new UnsupportedOperationException( |
| 101 | "No native library found for platform: " + PLATFORM_DIR + |
| 102 | ". Supported platforms can be found at https://vowpalwabbit.org/docs/vowpal_wabbit/java/"); |
| 103 | } |
| 104 | |
| 105 | // load the library |
| 106 | System.load(tempDirectory.resolve(nativesPrefix + LIB_NAME).toString()); |
| 107 | } |
| 108 | |
| 109 | static { |
| 110 | try { |