Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Can't run JNA with glibc #61

Closed
TimReset opened this issue Aug 5, 2023 · 6 comments
Closed

[Bug]: Can't run JNA with glibc #61

TimReset opened this issue Aug 5, 2023 · 6 comments
Labels
question Further information is requested

Comments

@TimReset
Copy link

TimReset commented Aug 5, 2023

Problem description

Hi!

I was trying to run JetBrains IDEs (IDEA, Clion) and got following error (it's not my post but I have exactly the same issue):
https://youtrack.jetbrains.com/issue/IDEA-280178

java.lang.UnsatisfiedLinkError: /data/data/com.termux/files/home/.cache/JetBrains/IdeaIC2021.2/tmp/jna11597614846036822681.tmp: dlopen failed: library "libc.so.6" not found at java.base/jdk.internal.loader.NativeLibraries.load(Native Method) at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:384) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:228) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:170) at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2389) at java.base/java.lang.Runtime.load0(Runtime.java:751) at java.base/java.lang.System.load(System.java:1912) at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:1019) at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:989) at com.sun.jna.Native.<clinit>(Native.java:195) at com.intellij.jna.JnaLoader.load(JnaLoader.java:18) at com.intellij.idea.StartupUtil.loadSystemLibraries(StartupUtil.java:844) at com.intellij.idea.StartupUtil.lambda$start$8(StartupUtil.java:226) at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1395) at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373) at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182) at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655) at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622) at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)

For my case it doesn't prevent the IDE running but it brakes some functions - eg debugging in Rust.

I created a minimal project to reproduce that issue:
jna_issue.zip

To run that need to install openjdk-17 and run:
java --class-path jna-5.13.0.jar JnaTest.java

And I installed glibc from pacman (this instruction https://wiki.termux.com/wiki/Switching_package_manager to use pacman, and pkg install glibc to install glibc)

So, what I understood:
jna11597614846036822681.tmp - it's lib from jna.jar file, looks like this one (jar is a zip archive, you can open it as zip):
jna-5.13.0.jar!\com\sun\jna\linux-aarch64\libjnidispatch.so

Or you can run to keep tmp file:
java --class-path jna-5.13.0.jar -Djnidispatch.preserve=true JnaTest.java

So, when I run that and check ldd I see reasonable reason of the error:
~ $ ldd .cache/JNA/temp/jna15778412403556095583.tmp
libc.so.6 => not found

But after running in proot env:
proot-distro login archlinux

I see the same error:
[root@localhost home]# java --class-path jna-5.13.0.jar -Djnidispatch.preserve=true JnaTest.java /data/data/com.termux/files/usr/java/packages/lib:/data/data/com.termux/files/usr/lib Exception in thread "main" java.lang.UnsatisfiedLinkError: /data/data/com.termux/files/home/.cache/JNA/temp/jna15872201178952457224.tmp: dlopen failed: library "libc.so.6" not found: needed by /data/data/com.termux/files/home/.cache/JNA/temp/jna15872201178952457224.tmp in namespace (default) at java.base/jdk.internal.loader.NativeLibraries.load(Native Method) at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:384) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:228) at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:170) at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2389) at java.base/java.lang.Runtime.load0(Runtime.java:751) at java.base/java.lang.System.load(System.java:1912) at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:1045) at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:1015) at com.sun.jna.Native.<clinit>(Native.java:221) at Main.main(JnaTest.java:7)

However ldd shows the lib:
[root@localhost home]# ldd /data/data/com.termux/files/home/.cache/JNA/temp/jna15872201178952457224.tmp ldd: warning: you do not have execution permission for /data/data/com.termux/files/home/.cache/JNA/temp/jna15872201178952457224.tmp'
libc.so.6 => /usr/lib/libc.so.6 (0x0000007156210000)
/usr/lib/ld-linux-aarch64.so.1 (0x0000003000000000)
/usr/lib/ld-linux-aarch64.so.1 (0x0000003000000000)`

Could you pls take a look? Maybe you have any idea how to fix that. It would be good to have ability to run JetBrains IDEs in Termux - they have a lot of good IDEs, not only for Java.

System information

termux-info:

@TimReset TimReset added the bug Something isn't working label Aug 5, 2023
@Maxython
Copy link
Member

Maxython commented Aug 6, 2023

Your problem is that you are using java based on bionic. For understanding: despite the fact that this glibc can work in Termux without proot, it is "separated" from the bionic libraries to avoid conflicts. This error can be corrected by using java based on glibc. I tried and got the following:

~ $ jdk-20.0.2/bin/java --class-path jna-5.13.0.jar JnaTest.java
/usr/java/packages/lib:/usr/lib64:/lib64:/lib:/usr/lib
8
~ $

If you want to install java based on glibc, here are the commands:

pacman -Syu
pacman -S gpkg-dev patchelf
grun --shell

# https://jdk.java.net/20/
wget https://download.java.net/java/GA/jdk20.0.2/6e380f22cbe7469fa75fb448bd903d8e/9/GPL/openjdk-20.0.2_linux-aarch64_bin.tar.gz

tar xf openjdk-20.0.2_linux-aarch64_bin.tar.gz
for i in jdk-20.0.2/bin/*; do
    grun --set $i
done
cp jdk-20.0.2/lib/libjli.so $GLIBC_PREFIX/lib

# done
jdk-20.0.2/bin/java --version

@Maxython Maxython closed this as completed Aug 6, 2023
@Maxython Maxython added question Further information is requested and removed bug Something isn't working labels Aug 6, 2023
@TimReset
Copy link
Author

TimReset commented Aug 7, 2023

Thanks a lot! I had suspicious about to use original java and patch that but I had no idea how to do that correctly.
I managed to run my example but when I run idea itself I get an error:

Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.UnsatisfiedLinkError: /data/data/com.termux/files/home/java_glibc/jdk-17.0.8/lib/libawt_xawt.so: libXext.so.6: cannot open shared object file: No such file or directory [in thread "DefaultDispatcher-worker-4"]
ldd for libawt_xawt.so shows:
ldd /data/data/com.termux/files/home/java_glibc/jdk-17.0.8/lib/libawt_xawt.so
ldd: warning: you do not have execution permission for /data/data/com.termux/files/home/java_glibc/jdk-17.0.8/lib/libawt_xawt.so' linux-vdso.so.1 (0x0000007f2a7eb000)
libm.so.6 => /data/data/com.termux/files/usr/glibc/lib/libm.so.6 (0x0000007f2a670000)
libawt.so => /data/data/com.termux/files/home/java_glibc/jdk-17.0.8/lib/libawt.so (0x0000007f2a580000)
libXext.so.6 => not found
libX11.so.6 => not found
libXrender.so.1 => not found
libdl.so.2 => /data/data/com.termux/files/usr/glibc/lib/libdl.so.2 (0x0000007f2a550000)
libXtst.so.6 => not found
libXi.so.6 => not found
libjava.so => /data/data/com.termux/files/home/java_glibc/jdk-17.0.8/lib/libjava.so (0x0000007f2a500000)
libjvm.so => not found
libpthread.so.0 => /data/data/com.termux/files/usr/glibc/lib/libpthread.so.0 (0x0000007f2a4d0000)
libc.so.6 => /data/data/com.termux/files/usr/glibc/lib/libc.so.6 (0x0000007f2a2c0000)
/data/data/com.termux/files/usr/glibc/lib/ld-linux-aarch64.so.1 (0x0000007f2a7ae000)
libjvm.so => not found
libjvm.so => not found`

I found libjvm.so in jdk/server/lib but couldn't find the rest of libs. I installed termux-x11 and libxext packages but it doesn't help. Could you please take a look? Maybe you will have some thoughts about that.

@Maxython
Copy link
Member

Maxython commented Aug 8, 2023

This error occurs because glibc-based graphics libraries were not found, and bionic-based graphics libraries will not work in this case. There are currently no graphics libraries in the gpkg-dev repo, they will be added to the gpkg repo which is currently being developed. The only thing you can do here is to try compiling the graphics libraries yourself with gcc-glibc.

@romanovj
Copy link

romanovj commented Aug 8, 2023

I used X libs from debian bookworm distro and was able to start xterminal, but only with root rights and selinux disabled, I was able to connect to XSDL app

@TimReset
Copy link
Author

TimReset commented Aug 8, 2023

Thanks for replies!

@TimReset
Copy link
Author

I finally managed to run CLion 2023.1.5 with debugger!
Screenshot_20230817_222152_VNC Viewer
Long story short:

  • need to build JNA lib in Termux. To do that, need to download it, install libffi.a (static libffi in Termux). JNA can't build own libffi.a - need to copy past it to expected path. Need to use the same JNA version as in IDEA. Need to use gcc, not clang.
  • after that JNA will work but pty4j won't work (it's a next error). Need to download pty4j and build native lib.
  • And need to change com.pty4j.unix.linux.OSFacadeImpl - move method com.pty4j.unix.linux.OSFacadeImpl.Linux_Util_lib#login_tty to com.pty4j.unix.linux.OSFacadeImpl.C_lib and remove com.pty4j.unix.linux.OSFacadeImpl#m_Utillib at all.
  • after these steps need to replace \lib\jna\aarch64\libjnidispatch.so and \lib\pty4j\linux\aarch64\libpty.so with your new versions.
  • and put your new pty4j into lib folder and add this lib as first lib in classpath in bin\idea.sh:
    CLASS_PATH="$IDE_HOME/lib/pty4j.0.12.13.jar:$IDE_HOME/lib/app.jar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants