Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
Loader.java
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package com.google.ortools;
15
16import com.sun.jna.Platform;
17import java.io.IOException;
18import java.net.URI;
19import java.net.URISyntaxException;
20import java.net.URL;
21import java.nio.file.FileSystem;
22import java.nio.file.FileSystemAlreadyExistsException;
23import java.nio.file.FileSystems;
24import java.nio.file.FileVisitResult;
25import java.nio.file.Files;
26import java.nio.file.Path;
27import java.nio.file.SimpleFileVisitor;
28import java.nio.file.attribute.BasicFileAttributes;
29import java.util.AbstractMap;
30import java.util.Arrays;
31import java.util.Collections;
32import java.util.List;
33import java.util.Map;
34import java.util.Objects;
35
37public class Loader {
38 private static final String RESOURCE_PATH = "ortools-" + Platform.RESOURCE_PREFIX + "/";
39
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));
46
47 URI resourceURI;
48 try {
49 resourceURI = resourceURL.toURI();
50 } catch (URISyntaxException e) {
51 throw new IOException(e);
52 }
53 return resourceURI;
54 }
55
56 @FunctionalInterface
57 private interface PathConsumer<T extends IOException> {
58 void accept(Path path) throws T;
59 }
60
67 private static Path unpackNativeResources(URI resourceURI) throws IOException {
68 Path tempPath;
69 tempPath = Files.createTempDirectory("ortools-java");
70 tempPath.toFile().deleteOnExit();
71
72 PathConsumer<?> visitor;
73 visitor = (Path sourcePath) -> Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
74 @Override
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;
80 }
81
82 @Override
83 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
84 throws IOException {
85 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
86 Files.copy(dir, newPath);
87 newPath.toFile().deleteOnExit();
88 return FileVisitResult.CONTINUE;
89 }
90 });
91
92 FileSystem fs;
93 try {
94 fs = FileSystems.newFileSystem(resourceURI, Collections.emptyMap());
95 } catch (FileSystemAlreadyExistsException e) {
96 fs = FileSystems.getFileSystem(resourceURI);
97 if (fs == null) {
98 throw new IllegalArgumentException();
99 }
100 }
101 Path p = fs.provider().getPath(resourceURI);
102 visitor.accept(p);
103 return tempPath;
104 }
105
107 private static boolean loaded = false;
108
109 public static synchronized void loadNativeLibraries() {
110 // prints the name of the Operating System
111 // System.out.println("OS: " + System.getProperty("os.name"));
112 if (loaded) {
113 return;
114 }
115 try {
116 // System.out.println("System.loadLibrary(\"jniortools\")");
117 System.loadLibrary("jniortools");
118 loaded = true;
119 return;
120 } catch (UnsatisfiedLinkError e) {
121 // Do nothing.
122 // System.out.println("Can't System.loadLibrary(jniortools)");
123 }
124 try {
125 URI resourceURI = getNativeResourceURI();
126 Path tempPath = unpackNativeResources(resourceURI);
127 // Load the native library
128 // System.out.println("System.load(" + System.mapLibraryName("jniortools") + ")");
129 System.load(tempPath.resolve(RESOURCE_PATH)
130 .resolve(System.mapLibraryName("jniortools"))
131 .toAbsolutePath()
132 .toString());
133 loaded = true;
134 return;
135 } catch (IOException | UnsatisfiedLinkError e) {
136 // Do nothing.
137 // System.out.println("Can't System.load(jniortools)");
138 }
139
140 // On windows, try to load each libraries one by one.
141 // System.out.println("Prefix: " + Platform.RESOURCE_PREFIX);
142 if (Platform.RESOURCE_PREFIX.equals("win32-x86-64")) {
143 try {
144 URI resourceURI = getNativeResourceURI();
145 Path tempPath = unpackNativeResources(resourceURI);
146 // libraries order does matter <LibraryName, isMandatory> !
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)));
158
159 for (Map.Entry<String, Boolean> dll : dlls) {
160 try {
161 // System.out.println("System.load(" + dll.getKey() + ")");
162 System.load(tempPath.resolve(RESOURCE_PATH)
163 .resolve(System.mapLibraryName(dll.getKey()))
164 .toAbsolutePath()
165 .toString());
166 } catch (UnsatisfiedLinkError e) {
167 System.out.println("System.load(" + dll.getKey() + ") failed!");
168 if (dll.getValue()) {
169 throw new RuntimeException(e);
170 }
171 }
172 }
173 loaded = true;
174 return;
175 } catch (IOException e) {
176 // Do nothing.
177 // System.out.println("unpack failed");
178 }
179 }
180 }
181}
static synchronized void loadNativeLibraries()
Definition Loader.java:109