38 private static final String RESOURCE_PATH =
"ortools-" + Platform.RESOURCE_PREFIX +
"/";
41 private static URI getNativeResourceURI()
throws IOException {
42 ClassLoader loader =
Loader.class.getClassLoader();
43 URL resourceURL = loader.getResource(RESOURCE_PATH);
44 Objects.requireNonNull(resourceURL,
45 String.format(
"Resource %s was not found in ClassLoader %s", RESOURCE_PATH, loader));
49 resourceURI = resourceURL.toURI();
50 }
catch (URISyntaxException e) {
51 throw new IOException(e);
67 private static Path unpackNativeResources(URI resourceURI)
throws IOException {
69 tempPath = Files.createTempDirectory(
"ortools-java");
70 tempPath.toFile().deleteOnExit();
73 visitor = (Path sourcePath) -> Files.walkFileTree(sourcePath,
new SimpleFileVisitor<Path>() {
75 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
76 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(file).toString());
77 Files.copy(file, newPath);
78 newPath.toFile().deleteOnExit();
79 return FileVisitResult.CONTINUE;
83 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
85 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
86 Files.copy(dir, newPath);
87 newPath.toFile().deleteOnExit();
88 return FileVisitResult.CONTINUE;
94 fs = FileSystems.newFileSystem(resourceURI, Collections.emptyMap());
95 }
catch (FileSystemAlreadyExistsException e) {
96 fs = FileSystems.getFileSystem(resourceURI);
98 throw new IllegalArgumentException();
101 Path p = fs.provider().getPath(resourceURI);
107 private static boolean loaded =
false;
117 System.loadLibrary(
"jniortools");
120 }
catch (UnsatisfiedLinkError e) {
125 URI resourceURI = getNativeResourceURI();
126 Path tempPath = unpackNativeResources(resourceURI);
129 System.load(tempPath.resolve(RESOURCE_PATH)
130 .resolve(System.mapLibraryName(
"jniortools"))
135 }
catch (IOException | UnsatisfiedLinkError e) {
142 if (Platform.RESOURCE_PREFIX.equals(
"win32-x86-64")) {
144 URI resourceURI = getNativeResourceURI();
145 Path tempPath = unpackNativeResources(resourceURI);
147 List<Map.Entry<String, Boolean>> dlls =
148 Arrays.asList((
new AbstractMap.SimpleEntry(
"zlib1",
true)),
149 (
new AbstractMap.SimpleEntry(
"bz2",
true)),
150 (
new AbstractMap.SimpleEntry(
"abseil_dll",
true)),
151 (
new AbstractMap.SimpleEntry(
"re2",
true)),
152 (
new AbstractMap.SimpleEntry(
"libutf8_validity",
true)),
153 (
new AbstractMap.SimpleEntry(
"libprotobuf",
true)),
154 (
new AbstractMap.SimpleEntry(
"highs",
false)),
155 (
new AbstractMap.SimpleEntry(
"libscip",
false)),
156 (
new AbstractMap.SimpleEntry(
"ortools",
true)),
157 (
new AbstractMap.SimpleEntry(
"jniortools",
true)));
159 for (Map.Entry<String, Boolean> dll : dlls) {
162 System.load(tempPath.resolve(RESOURCE_PATH)
163 .resolve(System.mapLibraryName(dll.getKey()))
166 }
catch (UnsatisfiedLinkError e) {
167 System.out.println(
"System.load(" + dll.getKey() +
") failed!");
168 if (dll.getValue()) {
169 throw new RuntimeException(e);
175 }
catch (IOException e) {