我正在设置一个java框架,它应该使用Google OR-Tools。下面的代码编译成功,但在运行时抛出异常:
Exception in thread "main" java.lang.UnsatisfiedLinkError: com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get()I
at com.google.ortools.linearsolver.operations_research_linear_solverJNI.MPSolver_CLP_LINEAR_PROGRAMMING_get(Native Method)
at com.google.ortools.linearsolver.MPSolver$OptimizationProblemType.<clinit>(MPSolver.java:221)
at Main.main(Main.java:15)我在Windows10上使用Intellij 2018.3。我花了很多时间尝试运行,但没有成功。根据我在互联网上的发现,异常可能是由于OR-Tools所依赖的外部库链接不良和/或缺少外部库而导致的。然而,我没有解决这个问题的背景,而且Intellij也没有突出显示任何东西。知道问题出在哪里吗?
为了完成,我运行以下代码:
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
public final class Main {
public static void main(String[] args) {
// Create the linear solver with the GLOP backend.
MPSolver solver =
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
// Create the variables x and y.
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
System.out.println("Number of variables = " + solver.numVariables());
// Create a linear constraint, 0 <= x + y <= 2.
MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
ct.setCoefficient(x, 1);
ct.setCoefficient(y, 1);
System.out.println("Number of constraints = " + solver.numConstraints());
// Create the objective function, 3 * x + y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 3);
objective.setCoefficient(y, 1);
objective.setMaximization();
solver.solve();
System.out.println("Solution:");
System.out.println("Objective value = " + objective.value());
System.out.println("x = " + x.solutionValue());
System.out.println("y = " + y.solutionValue());
}
}发布于 2019-11-14 21:43:55
免责声明:与其说是一个答案,不如说是一个很长的评论...
注意:我假设你使用的是github或-tools存储库,如果你使用的是二进制包,它应该大致相同……
1)您必须加载jni库,该库将加载OR-Tools C++库及其依赖项……
/** Simple linear programming example.*/
public class Main {
static {
System.loadLibrary("jniortools");
}
public static void main(String[] args) throws Exception {2)你有没有设法运行java示例?
make run SOURCE=ortools/linear_solver/samples/SimpleLpProgram.java参考:https://developers.google.com/optimization/introduction/java#simple_example
3)正如Kayaman所指出的,您必须传递java运行时可以找到本地库的文件夹(即JNI包装器jniortools.dll及其依赖项libortools.dll)。
如果您查看控制台日志,您将看到完整的命令行:
java -Xss2048k -Djava.library.path=lib -cp lib\sample.jar;lib\com.google.ortools.jar;lib\protobuf.jar ...\sample它来自makefiles/Makefile.java文件:
JAVAFLAGS = -Djava.library.path=$(LIB_DIR)
...
ifeq ($(SOURCE_SUFFIX),.java) # Those rules will be used if SOURCE contain a .java file
$(CLASS_DIR)/$(SOURCE_NAME): $(SOURCE) $(JAVA_OR_TOOLS_LIBS) | $(CLASS_DIR)
-$(DELREC) $(CLASS_DIR)$S$(SOURCE_NAME)
-$(MKDIR_P) $(CLASS_DIR)$S$(SOURCE_NAME)
"$(JAVAC_BIN)" -d $(CLASS_DIR)$S$(SOURCE_NAME) \
-cp $(LIB_DIR)$Scom.google.ortools.jar$(CPSEP)$(LIB_DIR)$Sprotobuf.jar \
$(SOURCE_PATH)
...
.PHONY: run # Run a Java program.
run: build
"$(JAVA_BIN)" -Xss2048k $(JAVAFLAGS) \
-cp $(LIB_DIR)$S$(SOURCE_NAME)$J$(CPSEP)$(LIB_DIR)$Scom.google.ortools.jar$(CPSEP)$(LIB_DIR)$Sprotobuf.jar \
$(SOURCE_NAME) $(ARGS)
endif源:https://github.com/google/or-tools/blob/46173008fdb15dae1dca0e8fa42a21ed6190b6e4/makefiles/Makefile.java.mk#L15和https://github.com/google/or-tools/blob/46173008fdb15dae1dca0e8fa42a21ed6190b6e4/makefiles/Makefile.java.mk#L328-L333
注意:您可以运行make detect_java来了解标志,即LIB_DIR的值注意:如果您确实使用了预编译包,Makefile在这里:https://github.com/google/or-tools/blob/stable/tools/Makefile.cc.java.dotnet
然后,您可以尝试在Intellij中添加此选项后...
您必须理解,or-tools是一组使用SWIG生成器封装到C++中的Java本机库。
发布于 2020-06-05 14:05:21
要使用Intellij (在windows机器上)使其工作,您需要:
intellij安装Microsoft Visual C++ Redistributable for Visual Studio
-Djava.library.path=<path to the lib folder that hold the jars>static { System.loadLibrary("jniortools"); }
发布于 2021-10-04 15:53:05
在我的例子中,解决方案很简单--我只需要添加以下一行代码:
Loader.loadNativeLibraries();加载器来自com.google.ortools.Loader的位置
https://stackoverflow.com/questions/58819070
复制相似问题