Skip to content

Commit

Permalink
[java] Java 21 build support (#19876)
Browse files Browse the repository at this point in the history
### Description
Bump spotless and the Gradle wrapper to 6.25.0 and 8.6 respectively to
allow compiling ORT on Java 21. The build still targets Java 8.

I'm not sure if there will be CI changes necessary to use this PR,
specifically for the Gradle version as I don't know if that is cached
somewhere earlier in the CI build process.

The new Gradle version adds a warning that using `--source` and
`--target` to select the Java language version is obsolete which is
annoying, we can fix it if we decide to only allow building on newer
versions of Java, while still supporting running on Java 8.

### Motivation and Context
Java 21 is the latest LTS release of Java and ORT should be able to
build on it.
  • Loading branch information
Craigacp authored Mar 28, 2024
1 parent f7b52d2 commit 2f82400
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 17 deletions.
5 changes: 3 additions & 2 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Use the main project's [build instructions](https://www.onnxruntime.ai/docs/how-

#### Requirements

JDK version 8 or later is required.
Java 11 or later is required to build the library. The compiled jar file will run on Java 8 or later.

The [Gradle](https://gradle.org/) build system is used here to manage the Java project's dependency management, compilation, testing, and assembly.
In particular, the Gradle [wrapper](https://docs.gradle.org/current/userguide/gradle_wrapper.html) at `java/gradlew[.bat]` is used, locking the Gradle version to the one specified in the `java/gradle/wrapper/gradle-wrapper.properties` configuration.
Expand All @@ -35,6 +35,7 @@ This allows the CMake system to ensure all of the C/C++ compilation is achieved
The Java build depends on C/C++ onnxruntime shared library and a C JNI shared library (source located in the `src/main/native` directory).
The JNI shared library is the glue that allows for Java to call functions in onnxruntime shared library.
Given the fact that CMake injects native dependencies during CMake builds, some gradle tasks (primarily, `build`, `test`, and `check`) may fail.
To run the Java build independently of CMake supply `-DcmakeBuildDir=<path-to-onnx-runtime-build-dir>`, though this will only succeed after an initial build of the native libraries has completed.

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.
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.
Expand All @@ -61,4 +62,4 @@ Then the corresponding C files in `./src/main/native/ai_onnxruntime*.c` may be u

### Dependencies

The Java API does not have any runtime or compile dependencies currently.
The Java API does not have any runtime or compile dependencies.
2 changes: 1 addition & 1 deletion java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'maven-publish'
id 'signing'
id 'jacoco'
id "com.diffplug.spotless" version "6.13.0"
id "com.diffplug.spotless" version "6.25.0"
}

allprojects {
Expand Down
Binary file modified java/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 3 additions & 2 deletions java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=1b6b558be93f29438d3df94b7dfee02e794b94d9aca4611a92cdb79b6b88e909
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.1-bin.zip
distributionSha256Sum=9631d53cf3e74bfa726893aee1f8994fee4e060c401335946dba2156f440f24c
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
29 changes: 17 additions & 12 deletions java/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,26 +131,29 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -197,11 +198,15 @@ if "$cygwin" || "$msys" ; then
done
fi

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
2 changes: 2 additions & 0 deletions java/src/main/java/ai/onnxruntime/OnnxJavaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public enum OnnxJavaType {

/** The native value of the enum. */
public final int value;

/** The Java side type used as the carrier. */
public final Class<?> clazz;

/** The number of bytes used by a single value of this type. */
public final int size;

Expand Down
1 change: 1 addition & 0 deletions java/src/main/java/ai/onnxruntime/OnnxMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum OnnxMapValueType {
FLOAT(3),
/** A 64-bit floating point value. */
DOUBLE(4);

/** The native enum value. */
final int value;

Expand Down
6 changes: 6 additions & 0 deletions java/src/main/java/ai/onnxruntime/OnnxRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,25 @@ final class OnnxRuntime {

/** The short name of the ONNX runtime shared library */
static final String ONNXRUNTIME_LIBRARY_NAME = "onnxruntime";

/** The short name of the ONNX runtime JNI shared library */
static final String ONNXRUNTIME_JNI_LIBRARY_NAME = "onnxruntime4j_jni";

/** The short name of the ONNX runtime shared provider library */
static final String ONNXRUNTIME_LIBRARY_SHARED_NAME = "onnxruntime_providers_shared";

/** The short name of the ONNX runtime CUDA provider library */
static final String ONNXRUNTIME_LIBRARY_CUDA_NAME = "onnxruntime_providers_cuda";

/** The short name of the ONNX runtime ROCM provider library */
static final String ONNXRUNTIME_LIBRARY_ROCM_NAME = "onnxruntime_providers_rocm";

/** The short name of the ONNX runtime DNNL provider library */
static final String ONNXRUNTIME_LIBRARY_DNNL_NAME = "onnxruntime_providers_dnnl";

/** The short name of the ONNX runtime OpenVINO provider library */
static final String ONNXRUNTIME_LIBRARY_OPENVINO_NAME = "onnxruntime_providers_openvino";

/** The short name of the ONNX runtime TensorRT provider library */
static final String ONNXRUNTIME_LIBRARY_TENSORRT_NAME = "onnxruntime_providers_tensorrt";

Expand Down
1 change: 1 addition & 0 deletions java/src/main/java/ai/onnxruntime/OnnxSparseTensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public abstract static class SparseTensor<T extends Buffer> {

/** The buffer holding the indices. */
final T indices;

/** The buffer holding the values. */
final Buffer values;

Expand Down
1 change: 1 addition & 0 deletions java/src/test/java/sample/ScoreMNIST.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public class ScoreMNIST {

private static final Logger logger = Logger.getLogger(ScoreMNIST.class.getName());

/** Pattern for splitting libsvm format files. */
private static final Pattern splitPattern = Pattern.compile("\\s+");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ jobs:
inputs:
versionSpec: '18.x'

- task: JavaToolInstaller@0
inputs:
versionSpec: "11"
jdkArchitectureOption: "x64"
jdkSourceOption: 'PreInstalled'

- template: set-version-number-variables-step.yml

- template: use-xcode-version.yml
Expand Down

0 comments on commit 2f82400

Please sign in to comment.