Maven: Version test

This example prints the version of sherpa-onnx and can be used to verify that the Java API and the native libraries are installed correctly.

It is the Java version of VersionTest.java in sherpa-onnx/java-api-examples.

Requirements

  • JDK 11 or above

  • Maven 3.x

Warning

The sherpa-onnx Java packages are hosted on JitPack, so you need to add the JitPack repository.

Step 1. Create the project directory

mkdir sherpa-onnx-java-demo
cd sherpa-onnx-java-demo

Step 2. Create the build file

Save the following as pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>sherpa-onnx-java-example</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.release>11</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <sherpa-onnx.version>v2.0.1</sherpa-onnx.version>
    </properties>

    <repositories>
        <!-- sherpa-onnx packages are hosted on JitPack -->
        <repository>
            <id>jitpack.io</id>
            <url>https://jitpack.io</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.github.k2-fsa</groupId>
            <artifactId>sherpa-onnx</artifactId>
            <version>${sherpa-onnx.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>VersionTest</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.5.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Step 3. Save the code

Save the following as src/main/java/VersionTest.java:

// Copyright 2025 Xiaomi Corporation

import com.k2fsa.sherpa.onnx.*;

public class VersionTest {
  public static void main(String[] args) {
    System.out.printf("sherpa-onnx version: %s\n", VersionInfo.getVersion());
    System.out.printf("sherpa-onnx gitSha1: %s\n", VersionInfo.getGitSha1());
    System.out.printf("sherpa-onnx gitDate: %s\n", VersionInfo.getGitDate());
    System.out.printf("onnxruntime version: %s\n", VersionInfo.getOnnxruntimeVersion());
  }
}

Step 4. Check the project layout

Your project directory should now look like this:

$ tree .
.
├── pom.xml
└── src
    └── main
        └── java
            └── VersionTest.java

4 directories, 2 files

$ ls -lh
total 8
-rw-r--r--@ 1 fangjun  wheel   2.1K 19 Nov 14:00 pom.xml
drwxr-xr-x@ 3 fangjun  wheel    96B 19 Nov 14:00 src

Step 5. Build and run

mvn -q package
java -jar target/sherpa-onnx-java-example-1.0.jar

Output

sherpa-onnx version: 2.0.1
sherpa-onnx gitSha1: af2d8d81
sherpa-onnx gitDate: Mon Oct 26 14:57:12 2026
onnxruntime version: 1.28.2