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) |
|
macOS x64 (Intel) |
|
Linux x64 |
|
Linux ARM64 |
|
Windows x64 |
|
Windows 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
Android
Maven
- Maven: Version test
- Maven: Offline ASR
- Maven: Streaming ASR
- Maven: TTS
- Maven: VAD
- Maven: Forced alignment
- Maven: Keyword spotting
- Maven: Punctuation
- Maven: Diacritics
- Maven: Speaker identification
- Maven: Audio tagging
- Maven: Speech enhancement
- Maven: Spoken language identification
- Maven: Speaker diarization
- Maven: Inverse text normalization
- Maven: WebSocket client
Gradle
- Gradle: Version test
- Gradle: Offline ASR
- Gradle: Streaming ASR
- Gradle: TTS
- Gradle: VAD
- Gradle: Forced alignment
- Gradle: Keyword spotting
- Gradle: Punctuation
- Gradle: Diacritics
- Gradle: Speaker identification
- Gradle: Audio tagging
- Gradle: Speech enhancement
- Gradle: Spoken language identification
- Gradle: Speaker diarization
- Gradle: Inverse text normalization
- Gradle: WebSocket client
Gradle KTS
- Gradle KTS: Version test
- Gradle KTS: Offline ASR
- Gradle KTS: Streaming ASR
- Gradle KTS: TTS
- Gradle KTS: VAD
- Gradle KTS: Forced alignment
- Gradle KTS: Keyword spotting
- Gradle KTS: Punctuation
- Gradle KTS: Diacritics
- Gradle KTS: Speaker identification
- Gradle KTS: Audio tagging
- Gradle KTS: Speech enhancement
- Gradle KTS: Spoken language identification
- Gradle KTS: Speaker diarization
- Gradle KTS: Inverse text normalization
- Gradle KTS: WebSocket client
Useful links
Java API examples: https://github.com/k2-fsa/sherpa-onnx/tree/master/java-api-examples
Maven examples: https://github.com/k2-fsa/sherpa-onnx/tree/master/java-api-examples/maven-examples
Gradle examples: https://github.com/k2-fsa/sherpa-onnx/tree/master/java-api-examples/gradle-examples
Gradle KTS examples: https://github.com/k2-fsa/sherpa-onnx/tree/master/java-api-examples/gradle-kts-examples
Java API source files: https://github.com/k2-fsa/sherpa-onnx/tree/master/sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx