Introduction
This error will occur on linux or Mac systems, when trying to execute any java command such as java -version
In this article, we will understand different reasons why below error
bash: /usr/bin/java: cannot execute binary file: Exec format error
might occur and possible ways to resolve it.
There might be many reasons of this error such as
1. Java architecture
Architecture of your java binary is different from machine architecture. Example, java binary is 32 bit while your system is 64 bit.
You can check the architecture of java binary with below command
$ file /usr/bin/java
Its output is somewhat as
/usr/bin/java: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=c0e68d88922d9aa1c102a8a25adcfce4f958e4f4…
The output shows that the architecture of the Java binary file is 64-bit. You can check if the installed system supports this architecture.
2. Install missing libraries
Java library has dependencies on other files. It might happen that some dependent libraries are missing on your system.
To check the dependencies of java library, use below command
$ ldd /usr/bin/java
The output of this command will show you the dependencies of java library as below
linux-vdso.so.1 => (0x00007fffa1f1a000)
libjli.so => /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/../lib/amd64/jli/libjli.so (0x00007f7b0f2a0000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7b0f0a5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7b0eddb000)
/lib64/ld-linux-x86-64.so.2 (0x00007f7b0f4a8000)
If any of these libraries are missing on your system, you will get Exec format error
.
Install them using the package manager for your Linux distribution.
For example, to install the libc6-i386 package on Ubuntu, you can use the following command:
$ sudo apt-get install libc6-i386
This command will install 32-bit version of the C library, which is required to execute 32-bit binary files on a 64-bit system.
You can reinstall or update the java runtime environment variable using below commands
$ sudo apt-get update $ sudo apt-get install default-jre
4. Check permission
Check if java binary file has execute permissions.
You can use the ls
command to check the file permissions as below
$ ls -l /usr/bin/java -rw-r--r-x 1 root root 7867 Feb 10 2023 /usr/bin/java
This output shows that the user does not have execute permissions on java binary.
To provide execute permission, use chmod command as below
$ sudo chmod +x /usr/bin/java
5. Check corrupted file
It may also happen that the java binary file is corrupted or damaged.
To ensure that it is not, use md5sum command to calculate the checksum of java binary as below
$ md5sum /usr/bin/java
Its output will be
3d1e6967b5c0g4j7b1a2efc9d9e3a9iu /usr/bin/java
If the checksum does not match the expected value, the file may be corrupt and you may need to reinstall it.
A symbolic link is like a short cut to the actual file.
It might happen that the java command is a symbolic link to the actual binary.
Ensure that it points to the correct location using below command
$ ls -l /usr/bin/java lrwxrwxrwx 1 root root 22 Feb 11 2023 /usr/bin/java -> /etc/alternatives/java $ ls -l /etc/alternatives/java lrwxrwxrwx 1 root root 73 Feb 11 2023 /etc/alternatives/java -> /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java
In this example, the java command is a symbolic link that points to the /etc/alternatives/java file, which in turn, is a symbolic link to the actual binary file.
If either of these links is broken or pointing to the wrong file, you may get the Exec format error
message.
To correct the symbolic link and point it to the correct location, use below command
$ sudo ln -fs /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/bin/java
Here,
ln, deletes the old java symbolic link and creates a new one that points to the /usr/lib/jvm/java-11-openjdk-amd64/bin/java file,
-f, option is to overwrite the existing symbolic link, if it exists, and
-s option is to create a symbolic link, rather than a hard link.
Hope this article will resolve the bash: /usr/bin/java: cannot execute binary file: Exec format error.