Java API

In this section, we describe how to use the Java API of sherpa-onnx on non-Android platforms, i.e., Linux, macOS and Windows.

The examples used in this section come from java-api-examples. For each function, e.g., ASR, TTS, VAD, forced alignment, etc., there is one page for each build tool: Maven, Gradle, and Gradle KTS. Every page is self-contained: it shows the project files, the Java code, how to download the model, how to run the example, and the expected output.

Please use the table of contents below (or at the right) to navigate to the page you are interested in.

Requirements

  • JDK 11 or above

  • One of

    • Maven 3.x

    • Gradle 8.x+ (or use the Gradle wrapper included in the example projects)

Supported platforms

  • Linux (x64)

  • Linux (arm64)

  • macOS (x64)

  • macOS (arm64)

  • Windows (x64)

  • Windows (arm64)

Dependencies

The sherpa-onnx Java packages are hosted on JitPack, so you need to add the JitPack repository in your project. There are two ways to declare the dependencies.

Approach 1: simple (single dependency)

This is the easiest way — one dependency pulls in everything. The pages below use this approach to keep the build files simple:

<dependency>
    <groupId>com.github.k2-fsa</groupId>
    <artifactId>sherpa-onnx</artifactId>
    <version>v2.0.1</version>
</dependency>

Note that it bundles the native libraries for every platform, so the resulting jar is very large (about 95 MB compressed).

Approach 2: multi-module (smaller jars)

This is useful for production use. It splits the JVM API and the native libraries so you only ship the platform you need:

<!-- 1. JVM core API -->
<dependency>
    <groupId>com.github.k2-fsa.sherpa-onnx</groupId>
    <artifactId>sherpa-onnx-jvm</artifactId>
    <version>v2.0.1</version>
</dependency>

<!-- 2. Platform native lib, pick ONE for your target platform -->
<dependency>
    <groupId>com.github.k2-fsa.sherpa-onnx</groupId>
    <artifactId>sherpa-onnx-native-lib-osx-aarch64</artifactId>
    <version>v2.0.1</version>
</dependency>

The available native library artifacts are:

Platform

Artifact

macOS ARM64 (Apple Silicon)

sherpa-onnx-native-lib-osx-aarch64

macOS x64 (Intel)

sherpa-onnx-native-lib-osx-x64

Linux x64

sherpa-onnx-native-lib-linux-x64

Linux ARM64

sherpa-onnx-native-lib-linux-aarch64

Windows x64

sherpa-onnx-native-lib-win-x64

Windows ARM64

sherpa-onnx-native-lib-win-arm64

The pages below use approach 1. If you want to ship only one platform and keep the jar small, please switch to approach 2.

Examples