0

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();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Kaushal
  • 1
  • 4
  • Use import ai.onnxruntime.OrtSession.Result to import a single class Result. Use ai.onnxruntime.OrtSession.* to import all Classes from a package. Use import static ai.onnxruntime.OrtSession.Result.* to import all static Methods from the Result Class. – TomStroemer Feb 16 '23 at 16:25
  • If you're compiling with `javac`, then you're not using the Maven pom.xml, and given your `javac` commandline doesn't specify a classpath, the library can't be found. You need to compile using Maven, or you need to manually specify the correct classpath when compiling (and running!) your code. – Mark Rotteveel Apr 13 '23 at 09:19

1 Answers1

0

You need to add the dependency to the classpath when calling the java compiler (-cp parameter), however since Java 11 you can directly call the java interpreter with single source files (JEP 330).

For Windows you can write something like

[path-to-main] % java -cp "C:\\Users\\XXXX\\.m2\\repository\\com\\microsoft\\onnxruntime\\onnxruntime\\1.13.1\\onnxruntime-1.13.1.jar" Main.java
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Marslinho
  • 1
  • 3