OR-Tools Maven for Java を使用する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
はじめに
このガイドでは、簡単な作業例を用いて、Java の OR-Tools の使い方について説明します。
この手順は他の Linux バリアントでも動作する可能性がありますが、以下の要件を満たすマシンでのみテストしています。
- Alpine Edge 64 ビット(x86_64)
- Centos 7 LTS 64-bit(x86_64)
- Debian SID 64 ビット(x86_64)
- Debian 11(bullseye)64 ビット(x86_64)
- Fedora 38 64 ビット(x86_64)
- Fedora 37 64-bit(x86_64)
- OpenSuse Leap 64 ビット(x86_64)
- Ubuntu 24.04 64 ビット(x86_64)
- Ubuntu 22.04 LTS 64 ビット(x86_64)
- Ubuntu 20.04 LTS 64 ビット(x86_64)
前提条件
以降のセクションでは、OR-Tools をインストールするための前提条件について説明します。
Java JDK 8.0 以上
Java JDK バージョン 8.0 以降をインストールするには、ターミナル ウィンドウを開いて次のように入力します。
Alpine
sudo apk add openjdk8
export JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
Centos
sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
Debian
sudo apt install -y default-jdk
export JAVA_HOME=/usr/lib/jvm/default-java
Fedora
sudo dnf install -y java-openjdk java-openjdk-devel
Fedora
sudo dnf install -y java-openjdk java-openjdk-devel
OpenSUSE
sudo zypper install -y java-1_8_0-openjdk java-1_8_0-openjdk-devel
Ubuntu
sudo apt install -y default-jdk
Ubuntu
sudo apt install -y default-jdk
Ubuntu
sudo apt install -y default-jdk
Maven 3.3 以降
Maven バージョン 3.3 以降をインストールするには、ターミナル ウィンドウを開いて次のように入力します。
Alpine
sudo apk add maven
Centos
sudo yum install -y maven
Debian
sudo apt install -y maven
Fedora
sudo dnf install -y maven
Fedora
sudo dnf install -y maven
OpenSUSE
sudo zypper install -y maven
Ubuntu
sudo apt install -y maven
Ubuntu
sudo apt install -y maven
Ubuntu
sudo apt install -y maven
次のコマンドを使用して、Maven が正しくインストールされていて Java があるかどうかをテストできます。
mvn -v
Maven を使用したインストール
Maven アプリケーションに OR-Tools を含めるには、そのアーティファクトへの依存関係をプロジェクトの pom.xml ファイルに追加します。たとえば、
<!-- https://mvnrepository.com/artifact/com.google.ortools/ortools-java -->
<dependency>
<groupId>com.google.ortools</groupId>
<artifactId>ortools-java</artifactId>
<version>9.10.4067</version>
</dependency>
Java サンプルコードを取得する
サンプルコードは java_or-tools リポジトリにあります。
リポジトリを ZIP ファイルとしてダウンロードして展開するか、リポジトリのクローンを作成します。
git clone -b v9.10 --depth 1 https://github.com/or-tools/java_or-tools
サンプル ディレクトリに移動します。
cd java_or-tools
サンプルをビルドする
java_or-tools
ディレクトリから:
次のコマンドを使用してプロジェクトをビルドします。
mvn compile -B
サンプルを実行する
java_or-tools
ディレクトリから:
次のコマンドを使用してバイナリを実行します。
mvn exec:java
これで完了です。OR-Tools を使用してアプリケーションを実行したので、OR-Tools の使用を開始する準備が整いました。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2024-08-09 UTC。
[null,null,["最終更新日 2024-08-09 UTC。"],[[["\u003cp\u003eThis guide provides instructions for setting up and running OR-Tools in Java on various Linux distributions.\u003c/p\u003e\n"],["\u003cp\u003eUsers must have Java JDK 8.0 or higher and Maven 3.3 or higher installed as prerequisites.\u003c/p\u003e\n"],["\u003cp\u003eOR-Tools can be easily integrated into Maven projects by adding a dependency to the project's \u003ccode\u003epom.xml\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe guide includes steps to download, build, and run a simple OR-Tools example application.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful completion, users can proceed to further explore and utilize OR-Tools functionalities.\u003c/p\u003e\n"]]],["This guide details how to set up and run OR-Tools in Java. First, install Java JDK (version 8.0 or higher) using the appropriate commands for your OS (Alpine, Centos, Debian, Fedora, OpenSUSE, or Ubuntu). Next, install Maven (version 3.3 or higher) with similar commands. To include OR-Tools, add the provided dependency to your `pom.xml` file. Download or clone the `java_or-tools` repository, then navigate to its directory. Finally, build and run the example using `mvn compile -B` and `mvn exec:java`.\n"],null,["# Using OR-Tools Maven for Java\n\nIntroduction\n------------\n\nThis guide gets you started with OR-Tools in Java with a simple working\nexample.\n\nAlthough these instructions might also work on other Linux variants, we\nhave only tested them on machines meeting the following requirements:\n\n\n- Alpine Edge 64-bit (x86_64)\n- Centos 7 LTS 64-bit (x86_64)\n- Debian SID 64-bit (x86_64)\n- Debian 11 (bullseye) 64-bit (x86_64)\n- Fedora 38 64-bit (x86_64)\n- Fedora 37 64-bit (x86_64)\n- OpenSuse Leap 64-bit (x86_64)\n- Ubuntu 24.10 64-bit (x86_64)\n- Ubuntu 22.04 LTS 64-bit (x86_64)\n- Ubuntu 20.04 LTS 64-bit (x86_64)\n\n\u003cbr /\u003e\n\nPrerequisites\n-------------\n\nThe following sections describe the prerequisites for installing OR-Tools.\n\n### Java JDK \\\u003e= 8.0\n\nTo install the Java JDK version 8.0 or higher, open a terminal window and enter: \n\n### Alpine\n\n sudo apk add openjdk8\n export JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk\n\n### Centos\n\n sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel\n\n### Debian\n\n sudo apt install -y default-jdk\n export JAVA_HOME=/usr/lib/jvm/default-java\n\n### Fedora\n\n sudo dnf install -y java-openjdk java-openjdk-devel\n\n### Fedora\n\n sudo dnf install -y java-openjdk java-openjdk-devel\n\n### OpenSUSE\n\n sudo zypper install -y java-1_8_0-openjdk java-1_8_0-openjdk-devel\n\n### Ubuntu\n\n sudo apt install -y default-jdk\n\n### Ubuntu\n\n sudo apt install -y default-jdk\n\n### Ubuntu\n\n sudo apt install -y default-jdk\n\n### Maven \\\u003e= 3.3\n\nTo install Maven version 3.3 or higher, open a terminal window and enter: \n\n### Alpine\n\n sudo apk add maven\n\n### Centos\n\n sudo yum install -y maven\n\n### Debian\n\n sudo apt install -y maven\n\n### Fedora\n\n sudo dnf install -y maven\n\n### Fedora\n\n sudo dnf install -y maven\n\n### OpenSUSE\n\n sudo zypper install -y maven\n\n### Ubuntu\n\n sudo apt install -y maven\n\n### Ubuntu\n\n sudo apt install -y maven\n\n### Ubuntu\n\n sudo apt install -y maven\n\nYou can test Maven is correctly installed and can find java using the following command: \n\n mvn -v\n\n\u003cbr /\u003e\n\nInstalling with Maven\n---------------------\n\nTo include OR-Tools in your Maven application, add a dependency on its artifacts\nto your project's pom.xml file. For example, \n\n \u003c!-- https://mvnrepository.com/artifact/com.google.ortools/ortools-java --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.ortools\u003c/groupId\u003e\n \u003cartifactId\u003eortools-java\u003c/artifactId\u003e\n \u003cversion\u003e9.12.4544\u003c/version\u003e\n \u003c/dependency\u003e\n\nGet the Java example code\n-------------------------\n\nThe example code is located in the\n[java_or-tools](https://github.com/or-tools/java_or-tools) repository.\n\n1. [Download the repository as a zip file](https://github.com/or-tools/java_or-tools/archive/v9.12.zip)\n and extract it, or clone the repository:\n\n git clone -b v9.12 --depth 1 https://github.com/or-tools/java_or-tools\n\n2. Change to the examples directory:\n\n cd java_or-tools\n\nBuild the example\n-----------------\n\nFrom the `java_or-tools` directory:\n\nBuild the project using:\n\n\n mvn compile -B\n\n\u003cbr /\u003e\n\nRun the example\n---------------\n\nFrom the `java_or-tools` directory:\n\nRun the binary using:\n\n\n mvn exec:java\n\n\u003cbr /\u003e\n\nCongratulations! You've just run an application with OR-Tools, you are ready to\n[get started with OR-Tools](../../introduction/java)."]]