Last active
August 13, 2021 03:06
-
-
Save tresf/0ffc6a58deb23e6002be94f414f4a080 to your computer and use it in GitHub Desktop.
Usb4Java Static Library Path Shim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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