Linux – Cannot execute binary in NixOS – No such file or directory

binarydynamic-linkinglinuxnixospatchelf

I tried to install the current oracle jre on a VM running NixOS.

Now the following happens:

[michas@cc:~]$ tar xvzf jre-7u40-linux-x64.tar.gz |grep bin/java
jre1.7.0_40/bin/javaws
jre1.7.0_40/bin/java_vm
jre1.7.0_40/bin/java

[michas@cc:~]$ ls -l ./jre1.7.0_40/bin/java
-rwxr-xr-x 1 michas nogroup 7750 Aug 27 09:17 ./jre1.7.0_40/bin/java

[michas@cc:~]$ ./jre1.7.0_40/bin/java
bash: ./jre1.7.0_40/bin/java: No such file or directory

WTF? The named file is obviously there. What is going on?

Trying to analyse further:

[michas@cc:~]$ strace ./jre1.7.0_40/bin/java
execve("./jre1.7.0_40/bin/java", ["./jre1.7.0_40/bin/java"], [/* 53 vars */]) = -1 ENOENT (No such file or directory)
write(2, "strace: exec: No such file or di"..., 40strace: exec: No such file or directory
) = 40
exit_group(1)                           = ?
+++ exited with 1 +++

[michas@cc:~]$ strace ./jre1.7.0_40/bin/jav
strace: Can't stat './jre1.7.0_40/bin/jav': No such file or directory

Ok, the output of a really missing file looks different.

[michas@cc:~]$ file ./jre1.7.0_40/bin/java
./jre1.7.0_40/bin/java: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, BuildID[sha1]=a558f547fe0b95fdc6a109cb7d9692d6d7969794, not stripped

[michas@cc:~]$ file ~/t
/home/michas/t: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, not stripped

The latter is a tiny self compiled binary running without problems.
Both look very similar. Hence format the binary itself seems to be fine.

[michas@cc:~]$ ldd ./jre1.7.0_40/bin/java
/run/current-system/sw/bin/ldd: line 116: ./jre1.7.0_40/bin/java: No such file or directory

Seems like there is a problem regarding the needed shared libraries.

What is going on and how can I fix it?

Best Answer

You can't usually run binary files in NixOS, they will either need some environment variables set or to be patched with patchElf. I assume you can install and run java using the nix package manager. You can probably also create a suitable environment to run it using myEnvFun.

Related Question