Google OR-Tools v9.12
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.Arrays;
30import java.util.Collections;
31import java.util.List;
32import java.util.Objects;
33
35public class Loader {
36 private static final String RESOURCE_PATH = "ortools-" + Platform.RESOURCE_PREFIX + "/";
37
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));
44
45 URI resourceURI;
46 try {
47 resourceURI = resourceURL.toURI();
48 } catch (URISyntaxException e) {
49 throw new IOException(e);
50 }
51 return resourceURI;
52 }
53
54 @FunctionalInterface
55 private interface PathConsumer<T extends IOException> {
56 void accept(Path path) throws T;
57 }
58
65 private static Path unpackNativeResources(URI resourceURI) throws IOException {
66 Path tempPath;
67 tempPath = Files.createTempDirectory("ortools-java");
68 tempPath.toFile().deleteOnExit();
69
70 PathConsumer<?> visitor;
71 visitor = (Path sourcePath) -> Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
72 @Override
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;
78 }
79
80 @Override
81 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
82 throws IOException {
83 Path newPath = tempPath.resolve(sourcePath.getParent().relativize(dir).toString());
84 Files.copy(dir, newPath);
85 newPath.toFile().deleteOnExit();
86 return FileVisitResult.CONTINUE;
87 }
88 });
89
90 FileSystem fs;
91 try {
92 fs = FileSystems.newFileSystem(resourceURI, Collections.emptyMap());
93 } catch (FileSystemAlreadyExistsException e) {
94 fs = FileSystems.getFileSystem(resourceURI);
95 if (fs == null) {
96 throw new IllegalArgumentException();
97 }
98 }
99 Path p = fs.provider().getPath(resourceURI);
100 visitor.accept(p);
101 return tempPath;
102 }
103
105 private static boolean loaded = false;
106
107 public static synchronized void loadNativeLibraries() {
108 // prints the name of the Operating System
109 // System.out.println("OS: " + System.getProperty("os.name"));
110 if (loaded) {
111 return;
112 }
113 try {
114 // System.out.println("System.loadLibrary(\"jniortools\")");
115 System.loadLibrary("jniortools");
116 loaded = true;
117 return;
118 } catch (UnsatisfiedLinkError e) {
119 // Do nothing.
120 // System.out.println("Can't System.loadLibrary(jniortools)");
121 }
122 try {
123 URI resourceURI = getNativeResourceURI();
124 Path tempPath = unpackNativeResources(resourceURI);
125 // Load the native library
126 // System.out.println("System.load(" + System.mapLibraryName("jniortools") + ")");
127 System.load(tempPath.resolve(RESOURCE_PATH)
128 .resolve(System.mapLibraryName("jniortools"))
129 .toAbsolutePath()
130 .toString());
131 loaded = true;
132 return;
133 } catch (IOException | UnsatisfiedLinkError e) {
134 // Do nothing.
135 // System.out.println("Can't System.load(jniortools)");
136 }
137
138 // On windows, try to load each libraries one by one.
139 // System.out.println("Prefix: " + Platform.RESOURCE_PREFIX);
140 if (Platform.RESOURCE_PREFIX.equals("win32-x86-64")) {
141 try {
142 URI resourceURI = getNativeResourceURI();
143 Path tempPath = unpackNativeResources(resourceURI);
144 // libraries order does matter !
145 List<String> dlls = Arrays.asList("zlib1", "abseil_dll", "re2", "utf8_validity",
146 "libprotobuf", "highs", "ortools", "jniortools");
147 for (String dll : dlls) {
148 try {
149 // System.out.println("System.load(" + dll + ")");
150 System.load(tempPath.resolve(RESOURCE_PATH)
151 .resolve(System.mapLibraryName(dll))
152 .toAbsolutePath()
153 .toString());
154 } catch (UnsatisfiedLinkError e) {
155 System.out.println("System.load(" + dll + ") failed!");
156 throw new RuntimeException(e);
157 }
158 }
159 loaded = true;
160 return;
161 } catch (IOException e) {
162 // Do nothing.
163 // System.out.println("unpack failed");
164 }
165 }
166 }
167}
static synchronized void loadNativeLibraries()
Definition Loader.java:107