SIGN IN SIGN UP
google / or-tools UNCLAIMED

Google's Operations Research tools:

0 0 3 C++
2022-03-24 17:19:17 +01:00
Linux | macOS | Windows
-------------------------------------------- | -------------------------------------------- | -------
[![Status][linux_java_svg]][linux_java_link] | [![Status][macos_java_svg]][macos_java_link] | [![Status][windows_java_svg]][windows_java_link]
2020-03-04 17:42:26 +01:00
[linux_java_svg]: https://github.com/google/or-tools/actions/workflows/cmake_linux_java.yml/badge.svg?branch=main
2021-10-27 17:42:45 +02:00
[linux_java_link]: https://github.com/google/or-tools/actions/workflows/cmake_linux_java.yml
[macos_java_svg]: https://github.com/google/or-tools/actions/workflows/cmake_macos_java.yml/badge.svg?branch=main
2021-10-27 17:42:45 +02:00
[macos_java_link]: https://github.com/google/or-tools/actions/workflows/cmake_macos_java.yml
[windows_java_svg]: https://github.com/google/or-tools/actions/workflows/cmake_windows_java.yml/badge.svg?branch=main
2021-10-27 17:42:45 +02:00
[windows_java_link]: https://github.com/google/or-tools/actions/workflows/cmake_windows_java.yml
2020-03-04 17:42:26 +01:00
# Introduction
2022-03-24 17:19:17 +01:00
First, verify you have the `JAVA_HOME` environment variable set otherwise CMake
and Maven won't be able to find your Java SDK.
2021-10-29 08:50:40 +02:00
## Build the Binary Package
2020-03-04 17:42:26 +01:00
2020-09-19 17:21:22 +02:00
To build the java maven packages, simply run:
2022-03-24 17:19:17 +01:00
2020-03-04 17:42:26 +01:00
```sh
cmake -S. -Bbuild -DBUILD_JAVA=ON
2020-09-19 17:21:22 +02:00
cmake --build build --target java_package -v
```
2022-03-24 17:19:17 +01:00
note: Since `java_package` is in target `all`, you can also omit the
2020-10-08 12:15:58 +02:00
`--target` option.
2020-09-19 17:21:22 +02:00
2021-10-29 08:50:40 +02:00
## Testing
2020-09-19 17:21:22 +02:00
To list tests:
2021-10-29 08:50:40 +02:00
2020-09-19 17:21:22 +02:00
```sh
cd build
ctest -N
```
To only run Java tests:
2021-10-29 08:50:40 +02:00
2020-09-19 17:21:22 +02:00
```sh
cd build
ctest -R "java_.*"
2020-03-04 17:42:26 +01:00
```
2021-10-29 08:50:40 +02:00
## Technical Notes
2022-03-24 17:19:17 +01:00
First you should take a look at the
[ortools/java/README.md](../../ortools/java/README.md) to understand the layout.
\
2020-10-08 12:15:58 +02:00
Here I will only focus on the CMake/SWIG tips and tricks.
2021-10-29 08:50:40 +02:00
### Build directory layout
2022-03-24 17:19:17 +01:00
Since Java use the directory layout and we want to use the
[CMAKE_BINARY_DIR](https://cmake.org/cmake/help/latest/variable/CMAKE_BINARY_DIR.html)
to generate the Java binary package.
2020-03-04 17:42:26 +01:00
2020-10-08 12:15:58 +02:00
We want this layout:
2021-10-29 08:50:40 +02:00
2020-03-04 17:42:26 +01:00
```shell
2020-10-08 12:15:58 +02:00
<CMAKE_BINARY_DIR>/java
├── ortools-linux-x86-64
│   ├── pom.xml
│   └── src/main/resources
│   └── linux-x86-64
│   ├── libjniortools.so
│   └── libortools.so
├── ortools-java
│   ├── pom.xml
│   └── src/main/java
│   └── com/google/ortools
│   ├── Loader.java
│   ├── linearsolver
│   │   ├── LinearSolver.java
│   │   └── ...
│   ├── constraintsolver
│   │   ├── ConstraintSolver.java
│   │   └── ...
2022-03-24 17:19:17 +01:00
│   ├── ...
2020-10-08 12:15:58 +02:00
│   └── sat
│   ├── CpModel.java
│   └── ...
├── constraint_solver
│   └── Tsp
│ ├── pom.xml
   └── src/main/java
   └── com/google/ortools
   └── Tsp.java
2020-03-04 17:42:26 +01:00
```
2022-03-24 17:19:17 +01:00
2020-10-08 12:15:58 +02:00
src: `tree build/java --prune -U -P "*.java|*.xml|*.so*" -I "target"`
2020-03-04 17:42:26 +01:00
2021-10-29 08:50:40 +02:00
### Managing SWIG generated files
2022-03-24 17:19:17 +01:00
You can use `CMAKE_SWIG_DIR` to change the output directory for the `.java` file
e.g.:
2021-10-29 08:50:40 +02:00
2020-03-04 17:42:26 +01:00
```cmake
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR}/..)
```
2022-03-24 17:19:17 +01:00
And you can use `CMAKE_LIBRARY_OUTPUT_DIRECTORY` to change the output directory
for the `.so` file e.g.:
2021-10-29 08:50:40 +02:00
2020-03-04 17:42:26 +01:00
```cmake
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/..)
```
2022-03-24 17:19:17 +01:00
[optional]You can use `SWIG_OUTFILE_DIR` to change the output directory for the
`.cxx` file e.g.:
2021-10-29 08:50:40 +02:00
2020-03-04 17:42:26 +01:00
```cmake
set(SWIG_OUTFILE_DIR ${CMAKE_CURRENT_BINARY_DIR}/..)
```
2022-03-24 17:19:17 +01:00
2020-03-04 17:42:26 +01:00
Then you only need to create a `pom.xml` file in `build/java` to be able to use
the build directory to generate the Java package.