1 package com.example.rustjni; 2 3 import com.google.devtools.build.runfiles.AutoBazelRepository; 4 import com.google.devtools.build.runfiles.Runfiles; 5 6 import com.sun.jna.Library; 7 import com.sun.jna.Native; 8 9 import java.io.IOException; 10 11 @AutoBazelRepository 12 public interface RustStringLength extends Library { calculate_string_length_from_rust(String s)13 long calculate_string_length_from_rust(String s); 14 loadNativeLibrary()15 static RustStringLength loadNativeLibrary() throws IOException { 16 String prefix = "lib"; 17 String extension = "so"; 18 if ("Mac OS X".equals(System.getProperty("os.name"))) { 19 extension = "dylib"; 20 } else if (System.getProperty("os.name").contains("Windows")) { 21 prefix = ""; 22 extension = "dll"; 23 } 24 Runfiles.Preloaded runfiles = Runfiles.preload(); 25 String dylibPath = runfiles.withSourceRepository(AutoBazelRepository_RustStringLength.NAME).rlocation("examples/ffi/java_calling_rust/rust-crate/" + prefix + "rstrlen." + extension); 26 27 return Native.load(dylibPath, RustStringLength.class); 28 } 29 } 30