36 private static final String RESOURCE_PATH =
"ortools-" + Platform.RESOURCE_PREFIX +
"/";
39 private static URI getNativeResourceURI()
throws IOException {
40 ClassLoader loader =
Loader.class.getClassLoader();
41 URL resourceURL = loader.getResource(RESOURCE_PATH);
42 Objects.requireNonNull(resourceURL,
43 String.format(
"Resource %s was not found in ClassLoader %s", RESOURCE_PATH, loader));
47 resourceURI = resourceURL.toURI();
48 }
catch (URISyntaxException e) {
49 throw new IOException(e);
65 private static Path unpackNativeResources(URI resourceURI)
throws IOException {
67 tempPath = Files.createTempDirectory(
"ortools-java");
68 tempPath.toFile().deleteOnExit();
71 visitor = (Path sourcePath) -> Files.walkFileTree(sourcePath,
new SimpleFileVisitor<Path>() {
73 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
74 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(file).toString());
75 Files.copy(file, newPath);
76 newPath.toFile().deleteOnExit();
77 return FileVisitResult.CONTINUE;
81 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
83 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
84 Files.copy(dir, newPath);
85 newPath.toFile().deleteOnExit();
86 return FileVisitResult.CONTINUE;
92 fs = FileSystems.newFileSystem(resourceURI, Collections.emptyMap());
93 }
catch (FileSystemAlreadyExistsException e) {
94 fs = FileSystems.getFileSystem(resourceURI);
96 throw new IllegalArgumentException();
99 Path p = fs.provider().getPath(resourceURI);
105 private static boolean loaded =
false;
115 System.loadLibrary(
"jniortools");
118 }
catch (UnsatisfiedLinkError e) {
123 URI resourceURI = getNativeResourceURI();
124 Path tempPath = unpackNativeResources(resourceURI);
127 System.load(tempPath.resolve(RESOURCE_PATH)
128 .resolve(System.mapLibraryName(
"jniortools"))
133 }
catch (IOException | UnsatisfiedLinkError e) {
140 if (Platform.RESOURCE_PREFIX.equals(
"win32-x86-64")) {
142 URI resourceURI = getNativeResourceURI();
143 Path tempPath = unpackNativeResources(resourceURI);
145 List<String> dlls = Arrays.asList(
"zlib1",
"abseil_dll",
"re2",
"utf8_validity",
146 "libprotobuf",
"highs",
"ortools",
"jniortools");
147 for (String dll : dlls) {
150 System.load(tempPath.resolve(RESOURCE_PATH)
151 .resolve(System.mapLibraryName(dll))
154 }
catch (UnsatisfiedLinkError e) {
155 System.out.println(
"System.load(" + dll +
") failed!");
156 throw new RuntimeException(e);
161 }
catch (IOException e) {