I haven't worked with Java much, and I need to load a model trained in Python and check if I can make inference in Java or not. I am trying to load an onnx file in Java. To do this I am importing onnx in Java, but it's throwing an error that package doesn't exist.
test_first % javac src/main/java/org/example/Main.java
src/main/java/org/example/Main.java:3: error: package ai.onnxruntime.OrtSession.Result does not exist
import ai.onnxruntime.OrtSession.Result.*;
^
1 error
I have added onnx as a dependency to my code:
<?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>org.example</groupId>
<artifactId>test_first</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime -->
<dependency>
<groupId>com.microsoft.onnxruntime</groupId>
<artifactId>onnxruntime</artifactId>
<version>1.13.1</version>
</dependency>
</dependencies>
</project>
After this run the pom.xml to get the dependency. I can see it in my external libraries: directory structure image
But when I try to compile the java code after adding the import statement it's giving me the above error.
I have also tried running:
mvn install:install-file -Dfile=/Users/XXXX/.m2/repository/com/microsoft/onnxruntime/onnxruntime/1.13.1/onnxruntime-1.13.1.jar -DgroupId=com.microsoft.onnxruntime -DartifactId=onnxruntime -Dversion=1.13.1 -Dpackaging=jar -DgeneratePom=true
But it says the artifact is already there in the local repository.
I am running the following code to check if import is successful for not.
package org.example;
import ai.onnxruntime.OrtEnvironment;
public class Main {
public static void main(String[] args) {
var env = OrtEnvironment.getEnvironment();
}
}