Skip to content

Commit

Permalink
Fix the windows API missing issue and Linux shared library size issue…
Browse files Browse the repository at this point in the history
… for Java packaging. (microsoft#774)

* Fix the java packaging issues

* add the jar path example for Linux build with a default configuration
  • Loading branch information
wenbingl authored Jul 29, 2024
1 parent c3145b8 commit c9c11b4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
12 changes: 12 additions & 0 deletions cmake/ext_java.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ file(GLOB onnxruntime_extensions4j_native_src
"${JAVA_ROOT}/src/main/native/*.h"
"${PROJECT_SOURCE_DIR}/include/*.h"
)
if(WIN32)
list(APPEND onnxruntime_extensions4j_native_src "${JAVA_ROOT}/ortx_jni.def")
endif()
# Build the JNI library
add_library(onnxruntime_extensions4j_jni SHARED ${onnxruntime_extensions4j_native_src})

Expand All @@ -70,6 +73,15 @@ else()
target_link_libraries(onnxruntime_extensions4j_jni PRIVATE ortcustomops)
endif()

if(LINUX)
set_property(TARGET onnxruntime_extensions4j_jni APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,--version-script -Wl,${JAVA_ROOT}/ortx_jni.ver")
# strip if not a debug build
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set_property(TARGET onnxruntime_extensions4j_jni APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-s")
endif()
endif()

standardize_output_folder(onnxruntime_extensions4j_jni)

# Set platform and arch for packaging
Expand Down
6 changes: 6 additions & 0 deletions java/ortx_jni.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBRARY "onnxruntime_extensions4j_jni.dll"
EXPORTS
RegisterCustomOps @1
AddExternalCustomOp @2
GetActiveOrtAPIVersion @3
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister @4
8 changes: 8 additions & 0 deletions java/ortx_jni.ver
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
global:
RegisterCustomOps;
AddExternalCustomOp;
GetActiveOrtAPIVersion;
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister;
local: *;
};
7 changes: 6 additions & 1 deletion tutorials/demo4j/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# How to start

1. copy JAR package from $REPO/out/$OS/RelWithDebInfo/java/build/libs/onnxruntime-extensions-${VERSION}.jar into app/libs folder.
1. correct the onnxruntime-extensions JAR location if needed, which is located at app/build.gradle: 16


`implementation fileTree(dir: '..\\..\\..\\out\\Windows\\java\\build\\libs', include: ['onnxruntime-extensions-0.*.0.jar'])
`

2. build and run this java project.
5 changes: 3 additions & 2 deletions tutorials/demo4j/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'

// onnxruntime and its extensions package
implementation 'com.microsoft.onnxruntime:onnxruntime:1.12.1'
implementation files('libs/onnxruntime-extensions-0.5.0.jar')
implementation 'com.microsoft.onnxruntime:onnxruntime:1.17.1'
implementation fileTree(dir: '..\\..\\..\\out\\Windows\\java\\build\\libs', include: ['onnxruntime-extensions-0.*.0.jar'])
// implementation fileTree(dir: '../../../out/Linux/RelWithDebInfo/java/build/libs', include: ['onnxruntime-extensions-0.*.0.jar'])

// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
Expand Down
12 changes: 10 additions & 2 deletions tutorials/demo4j/app/src/main/java/demo4j/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.util.Arrays;
import java.util.Map;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import ai.onnxruntime.*;
import ai.onnxruntime.extensions.OrtxPackage;
Expand All @@ -19,8 +20,15 @@ public String inference(){
sess_opt.registerCustomOpLibrary(OrtxPackage.getLibraryPath());

/* do a quick inference on Bert Tokenizer custom ops with Ort */
var modelPath = Paths.get(this.getClass().getClassLoader().getResource("test_bert_tokenizer.onnx").getPath());
var session = env.createSession(modelPath.toString(), sess_opt);
var modelPath = "";
try {
modelPath = Paths.get(this.getClass().getClassLoader().getResource("test_bert_tokenizer.onnx").toURI()).toString();
} catch (URISyntaxException e) {
// handle the exception
e.printStackTrace();
}
var session = env.createSession(modelPath, sess_opt);

var t1 = OnnxTensor.createTensor(env, new String[]{"This is a test"});
var inputs = Map.of("text", t1);
try (var r = session.run(inputs)) {
Expand Down

0 comments on commit c9c11b4

Please sign in to comment.