Skip to content

Instantly share code, notes, and snippets.

@tresf
Last active August 13, 2021 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tresf/0ffc6a58deb23e6002be94f414f4a080 to your computer and use it in GitHub Desktop.
Save tresf/0ffc6a58deb23e6002be94f414f4a080 to your computer and use it in GitHub Desktop.
Usb4Java Static Library Path Shim
package qz;
import com.sun.jna.Platform;
import org.usb4java.LibUsb;
import org.usb4java.Loader;
import qz.utils.SystemUtilities;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
public class LoaderShim {
public static void main(String ... args) throws Exception {
Field loaded = Loader.class.getDeclaredField("loaded");
loaded.setAccessible(true);
loaded.set(Loader.class, true);
Method getPlatform = Loader.class.getDeclaredMethod("getPlatform");
Method getLibName = Loader.class.getDeclaredMethod("getLibName");
Method getExtraLibName = Loader.class.getDeclaredMethod("getExtraLibName");
getPlatform.setAccessible(true);
getLibName.setAccessible(true);
getExtraLibName.setAccessible(true);
// Simulate path calculate like Loader does
//String platform = (String)getPlatform.invoke(loaderClass);
String lib = (String)getLibName.invoke(Loader.class);
String extraLib = (String)getExtraLibName.invoke(Loader.class);
//System.load(SystemUtilities.getAppPath().resolve("libs").resolve(extraLib != null ? extraLib : lib).toString());
//System.out.println(loaded.get(loaderClass));
//System.out.println(LibUsb.getVersion());
// We'll move all native libraries (including JNA) at packaging time using "externalize-libs"
// JNA is used heavily and is a good indicator
URL resourceUrl = SystemUtilities.class.getResource("/com/sun/jna/" + Platform.RESOURCE_PREFIX);
if(resourceUrl == null) {
System.out.println("JNA isn't in the jar, we're probably externalized");
} else {
System.out.println("JNA is in the jar, we're probably bundled");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment