java-native-access / jna

Java Native Access
Other
8.5k stars 1.68k forks source link

Shade/Relocation support #679

Open jikuja opened 8 years ago

jikuja commented 8 years ago

I tried HelloWorld example from Getting Started Guide and got following results:

Result of shadowjar, no relocation. Works as excepted:

jikuja@LG MINGW64 /d/projects/jna-relocation-bug
$ java -Djna.debug_load.jna=true -jar build/libs/jna-relocation-bug-1.0-SNAPSHOT-all.jar
Trying (via loadLibrary) jnidispatch
Looking in classpath from sun.misc.Launcher$AppClassLoader@4e25154f for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/D:/projects/jna-relocation-bug/build/libs/jna-relocation-bug-1.0-SNAPSHOT-all.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Trying C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna5363526492694878451.dll
Found jnidispatch at C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna5363526492694878451.dll
Hello, World

Shadowjar with relocation. Does not work:

jikuja@LG MINGW64 /d/projects/jna-relocation-bug
$ java -Djna.debug_load.jna=true -jar build/libs/jna-relocation-bug-1.0-SNAPSHOT-reloc.jar
Trying (via loadLibrary) jnidispatch
Looking in classpath from sun.misc.Launcher$AppClassLoader@4e25154f for /a/com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/D:/projects/jna-relocation-bug/build/libs/jna-relocation-bug-1.0-SNAPSHOT-reloc.jar!/a/com/sun/jna/win32-x86-64/jnidispatch.dll
Trying C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna2069879652875333160.dll
Found jnidispatch at C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna2069879652875333160.dll
Exception in thread "main" java.lang.UnsatisfiedLinkError: a.com.sun.jna.Native.sizeof(I)I
        at a.com.sun.jna.Native.sizeof(Native Method)
        at a.com.sun.jna.Native.<clinit>(Native.java:141)
        at HelloWorld$CLibrary.<clinit>(HelloWorld.java:13)
        at HelloWorld.main(HelloWorld.java:20)

Build script I used: https://gist.github.com/jikuja/10e29922c819e2a0eb16183a09a14f38

Bug or did I miss some important configuration flag to enable proper shading support?

twall commented 8 years ago

What's happening is that JNI is expecting to find a native method based on the package and class name of the containing Java class. You've effectively renamed the package/class, so JNI's "guess" of the native symbol is now not going to match.

JNA uses JNI's default symbol mapping to find native functions (such as Native.sizeof). You'd have to either avoid "relocating" those specific functions (and running the full suite of JNA tests to ensure you didn't break something else) or rewriting JNA's code which references native functions to do a different type of lookup of native methods (it's possible to take a Java method and explicitly bind it to an explicit native symbol, but it's not clear that would generally be a good thing for JNA to do, even if it makes your life easier in this specific case).

On Tue, Jul 12, 2016 at 12:05 PM, jikuja notifications@github.com wrote:

I tried HelloWorld example from Getting Started Guide and got following results:

Result of shadowjar, no relocation. Works as excepted:

jikuja@LG MINGW64 /d/projects/jna-relocation-bug $ java -Djna.debug_load.jna=true -jar build/libs/jna-relocation-bug-1.0-SNAPSHOT-all.jar Trying (via loadLibrary) jnidispatch Looking in classpath from sun.misc.Launcher$AppClassLoader@4e25154f for /com/sun/jna/win32-x86-64/jnidispatch.dll Found library resource at jar:file:/D:/projects/jna-relocation-bug/build/libs/jna-relocation-bug-1.0-SNAPSHOT-all.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll Trying C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna5363526492694878451.dll Found jnidispatch at C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna5363526492694878451.dll Hello, World

Shadowjar with relocation. Does not work:

jikuja@LG MINGW64 /d/projects/jna-relocation-bug $ java -Djna.debug_load.jna=true -jar build/libs/jna-relocation-bug-1.0-SNAPSHOT-reloc.jar Trying (via loadLibrary) jnidispatch Looking in classpath from sun.misc.Launcher$AppClassLoader@4e25154f for /a/com/sun/jna/win32-x86-64/jnidispatch.dll Found library resource at jar:file:/D:/projects/jna-relocation-bug/build/libs/jna-relocation-bug-1.0-SNAPSHOT-reloc.jar!/a/com/sun/jna/win32-x86-64/jnidispatch.dll Trying C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna2069879652875333160.dll Found jnidispatch at C:\Users\jikuja\AppData\Local\Temp\jna--1160004128\jna2069879652875333160.dll Exception in thread "main" java.lang.UnsatisfiedLinkError: a.com.sun.jna.Native.sizeof(I)I at a.com.sun.jna.Native.sizeof(Native Method) at a.com.sun.jna.Native.(Native.java:141) at HelloWorld$CLibrary.(HelloWorld.java:13) at HelloWorld.main(HelloWorld.java:20)

Build script I used: https://gist.github.com/jikuja/10e29922c819e2a0eb16183a09a14f38

Bug or did I miss some important configuration flag to enable proper shading support?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/java-native-access/jna/issues/679, or mute the thread https://github.com/notifications/unsubscribe/AAuMreuVk9jXz02KY5eUnENLNETqKhe3ks5qU7tjgaJpZM4JKjTX .

PumpkinXD commented 1 year ago

so, any ways to get around it?

I'm backporting a minecraft mod from mc1.12.2 to mc1.8.9

I want to use a newer version of jna(for extract feature) but jna3.4 is used in mc1.8.9 which don't have such feature and causing this issue