0

I am trying to install Java on a Linux server.

Linux server details

NAME="Oracle Linux Server"
VERSION="7.9"

System Architecture

x86_64

What I have tried so far

I have downloaded x64 Compressed Archive from https://download.oracle.com/java/24/latest/jdk-24_linux-x64_bin.tar.gz and extracted it in the /usr/java/ folder as a super user by using sudo su command. Then I updated the values of the PATH and JAVA_HOME variables in the .bashrc file. The echo $PATH command and echo $JAVA_HOME returned the below values.

/sbin:/bin:/usr/sbin:/usr/bin:/apps/local/go/bin:/apps/local/go/dev/bin:/usr/java/jdk-24/bin

/usr/java/jdk-24

Then I ran java -version to check the installation. But it returned below error.

bash: /usr/java/jdk-24/bin/java: cannot execute binary file

I did enough googling and tried the below workarounds

  • sudo update-alternatives --config java // it didn't return any value
  • chmod +x /usr/java/jdk-24/bin/java // didn't work

Then I navigated to my user folder (/home/XXX) and opened .bash_profile file:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export JAVA_HOME=/usr/local/lib/java/jdk-24
export PATH=$JAVA_HOME/bin:$PATH
export PATH

Then I ran source .bash_profile and then ran java -version, which showed the following:

java version "24" 2025-03-18
Java(TM) SE Runtime Environment (build 24+36-3646)
Java HotSpot(TM) 64-Bit Server VM (build 24+36-3646, mixed mode, sharing)

If I exit the root user and try again, it shows the "cannot execute binary file" error again.

Please help me to solve this issue.

2
  • 1
    Oracle Linux 7.9 is outdated (End-of-Life: July 2024). Do you want to develop on the Oracle database with Java? Wouldn't you prefer to use a pre-configured VirtualBox with all the packages you need? You can easily install and use it on Windows, Mac, Solaris, Linux distributions, etc. Commented Mar 25 at 17:18
  • 2
    Then your organisation should update the server. It's definitely not good practice to keep EOL systems running in a production environment Commented Mar 25 at 20:41

1 Answer 1

0

I think the most likely problem is that you're running (or rather, attempting to run) two different Java installations.

In your first example, the one that errors out,

JAVA_HOME=/usr/java/jdk-24

In the second example, where you successfully run java -version

JAVA_HOME=/usr/local/lib/java/jdk-24

Check the values and try again.

Some other points that may or may not be relevant here:

  • Environment variables would usually be set in ~/.profile or ~/.bash_profile, and not in ~/.bashrc
  • To get a root shell there's no need to use sudo su. Just sudo -s, or maybe sudo -i if you need a login shell, is quite sufficient
3
  • On many distros even sudo bash is sufficient to get a shell with privileges to create root-owned files/directories, edit root-owned files, and run commands like yum/rpm/apt successfully as root.
    – Sotto Voce
    Commented Mar 25 at 20:52
  • @SottoVoce yes, but I don't see the point. sudo su is an anti-pattern better served by either sudo -s or sudo -i. You could use sudo bash an alternative to the first but it's longer to type Commented Mar 25 at 21:28
  • True, bash is longer to type than -i or -s by two keystrokes, but the tradeoff is that, to the occasional sudo user (99.5% of everyone), the typing difference is tiny but the clarity of what happens is large. I wasn't trying to contradict your answer, just contributing a third command that gives the desired result.
    – Sotto Voce
    Commented Mar 26 at 1:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.