You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# this jar is solely used to signalling mechanism for dependency management in CMake
39
+
# if any of the Java sources change, the jar (and generated headers) will be regenerated and the onnxruntime4j_jni target will be rebuilt
40
+
add_custom_command(OUTPUT${JAVA_OUTPUT_JAR}COMMAND${GRADLE_EXECUTABLE} clean jar WORKING_DIRECTORY${JAVA_ROOT}DEPENDS${onnxruntime4j_gradle_files}${onnxruntime4j_src})
This directory contains the Java language binding for the ONNX runtime.
4
+
Java Native Interface (JNI) is used to allow for seamless calls to ONNX runtime from Java.
5
+
6
+
## Usage
7
+
8
+
TBD: maven distribution
9
+
10
+
The minimum supported Java Runtime is version 8.
11
+
12
+
An example implementation is located in [src/test/java/sample/ScoreMNIST.java](src/test/java/sample/ScoreMNIST.java)
13
+
14
+
This project can be built manually using the instructions below.
15
+
16
+
### Building
17
+
18
+
Use the main project's [build instructions](../BUILD.md) with the `--build_java` option.
19
+
20
+
#### Requirements
21
+
22
+
JDK version 8 or later is required.
23
+
The [Gradle](https://gradle.org/) build system is required and used here to manage the Java project's dependency management, compilation, testing, and assembly.
24
+
You may use your system Gradle installation installed on your PATH.
25
+
Version 6 or newer is recommended.
26
+
Optionally, you may use your own Gradle [wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html) which will be locked to a version specified in the `build.gradle` configuration.
27
+
This can be done once by using system Gradle installation to invoke the wrapper task in the java project's directory: `cd REPO_ROOT/java && gradle wrapper`
28
+
Any installed wrapper is gitignored.
29
+
30
+
#### Build Output
31
+
32
+
The build will generate output in `$REPO_ROOT/build/$OS/$CONFIGURATION/java/build`:
33
+
34
+
*`docs/javadoc/` - HTML javadoc
35
+
*`reports/` - detailed test results and other reports
36
+
*`libs/onnxruntime.jar` - JAR with classes, depends on `onnxruntime-jni.jar` and `onnxruntime-lib.jar `
37
+
*`libs/onnxruntime-jni.jar`- JAR with JNI shared library
38
+
*`libs/onnxruntime-lib.jar` - JAR with onnxruntime shared library
39
+
*`libs/onnxruntime-all.jar` - the 3 preceding jars all combined: JAR with classes, JNI shared library, and onnxruntime shared library
40
+
41
+
The reason the shared libraries are split out like that is that users can mix and match to suit their use case:
42
+
43
+
* To support a single OS/Architecture without any dependencies, use `libs/onnxruntime-all.jar`.
44
+
* To support cross-platform: bundle a single `libs/onnxruntime.jar` and with all of the respective `libs/onnxruntime-jni.jar` and `libs/onnxruntime-lib.jar` for all of the desired OS/Architectures.
45
+
* To support use case where an onnxruntime shared library will reside in the system's library search path: bundle a single `libs/onnxruntime.jar` and with all of the `libs/onnxruntime-jni.jar`. The onnxruntime shared library should be loaded using one of the other methods described in the "Advanced Loading" section below.
46
+
47
+
#### Build System Overview
48
+
49
+
The main CMake build system delegates building and testing to Gradle.
50
+
This allows the CMake system to ensure all of the C/C++ compilation is achieved prior to the Java build.
51
+
The Java build depends on C/C++ onnxruntime shared library and a C JNI shared library (source located in the `src/main/native` directory).
52
+
The JNI shared library is the glue that allows for Java to call functions in onnxruntime shared library.
53
+
Given the fact that CMake injects native dependencies during CMake builds, some gradle tasks (primarily, `build`, `test`, and `check`) may fail.
54
+
55
+
When running the build script, CMake will compile the `onnxruntime` target and the JNI glue `onnxruntime4j_jni` target and expose the resulting libraries in a place where Gradle can ingest them.
56
+
Upon successful compilation of those targets, a special Gradle task to build will be executed. The results will be placed in the output directory stated above.
57
+
58
+
### Advanced Loading
59
+
60
+
The default behavior is to load the shared libraries using classpath resources.
61
+
If your use case requires custom loading of the shared libraries, please consult the javadoc in the [package-info.java](src/main/java/ai/onnxruntime/package-info.java) or [OnnxRuntime.java](src/main/java/ai/onnxruntime/OnnxRuntime.java) files.
62
+
63
+
## Development
64
+
65
+
### Code Formatting
66
+
67
+
[Spotless](https://github.com/diffplug/spotless/tree/master/plugin-gradle) is used to keep the code properly formatted.
68
+
Gradle's `spotlessCheck` task will show any misformatted code.
69
+
Gradle's `spotlessApply` task will try to fix the formatting.
70
+
Misformatted code will raise failures when checks are ran during test run.
71
+
72
+
### JNI Headers
73
+
74
+
When adding or updating native methods in the Java files, it may be necessary to examine the relevant JNI headers in `build/headers/ai_onnxruntime*.h`.
75
+
These files can be manually generated using Gradle's `compileJava` task which will compile the Java and update the header files accordingly.
76
+
Then the corresponding C files in `./src/main/native/ai_onnxruntime*.c` may be updated and the build can be ran.
77
+
78
+
### Dependencies
79
+
80
+
The Java API does not have any runtime or compile dependencies currently.
0 commit comments