Intellij中的Google OR工具:UnsatisfiedLinkError [英] Google OR-Tools in Intellij: UnsatisfiedLinkError

查看:512
本文介绍了Intellij中的Google OR工具:UnsatisfiedLinkError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个应该使用Google OR-Tools的Java框架.下面的代码编译成功,但是在运行时引发异常:

I am setting up a java framework that should use the Google OR-Tools. The code below compiles successfully, but throws an exception at runtime:

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)

我在Windows 10上使用Intellij 2018.3.我花了很多时间尝试运行此程序,但未成功.根据我在互联网上发现的信息,该异常可能是由于链接不良和/或缺少OR-Tools依赖的外部库引起的.但是,我没有解决此问题的背景,而且Intellij也不突出显示任何内容.知道是什么问题吗?

I am using Intellij 2018.3 on Windows 10. I spent a lot of time trying to get this run, but unsuccessful. Based on what I found on the internet, the exception might be caused by poor linking and/or missing external libraries on which OR-Tools depends. However, I don't have the background to resolve this issue, and also Intellij does not highlight anything. Any idea what the problem is?

为完成起见,这是我运行的代码:

For completion, this is the code I run:

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());
  }
}

推荐答案

要使用Intellij(在Windows计算机上)使其工作,您需要:

To make it work using Intellij (over a windows machine) you need to:

  1. 安装 Microsoft Visual C ++可重新分发给Visual Studio
  2. 下载并提取用于Java的OR工具库
  3. 在intellij中,将将jar依赖项添加到提取文件的lib文件夹下的2个jar中(每个2个这就是为什么).
  4. 将lib库路径添加到VM选项.在Intellij中,编辑您的运行配置并添加到vm选项:-Djava.library.path=<path to the lib folder that hold the jars>
  5. 通过将以下代码添加到您的班级中来静态加载jni库(如所述

  1. Install Microsoft Visual C++ Redistributable for Visual Studio
  2. Download and extract the OR-Tools library for Java
  3. In intellij, add jar dependency to the 2 jars under the lib folder of the extracted files (each of the 2 jars separately, do not add to lib folder itself. This is why).
  4. Add the lib library path to VM options. In Intellij edit your run-configuration and add to vm options: -Djava.library.path=<path to the lib folder that hold the jars>
  5. Load the jni library statically by adding the below code to your class (as mentioned here.)

static { System.loadLibrary("jniortools"); }

static { System.loadLibrary("jniortools"); }

这篇关于Intellij中的Google OR工具:UnsatisfiedLinkError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆