0

I've edited my .bashrc file on my server, with sudo nano ~/.bashrc

export JAVA_HOME=/usr/bin/java
export PATH=${PATH}:${JAVA_HOME}/bin

I've re-logged in, and here is all the path as output:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# echo $JAVA_HOME
/usr/bin/java
root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/bin/java/bin

But when I try to run my spring boot server, it gives me:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# ./mvnw spring-boot:run
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/bin/java/bin/java

And when I try to do a mvn install, I get:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# mvn install
The JAVA_HOME environment variable is not defined correctly
This environment variable is needed to run this program
NB: JAVA_HOME should point to a JDK not a JRE

But here is the java configuration list:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# sudo update-alternatives --list java
/usr/lib/jvm/java-11-openjdk-amd64/bin/java
/usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java

And when I try to run(just tried, without installing dependencies), I get:

root@ubuntu-s-1vcpu-1gb-blr1-01:~/maifee/backend-spring# ./mvnw spring-boot:run
Error: JAVA_HOME is not defined correctly.
  We cannot execute /usr/bin/java/bin/java

How can I set up my environment to correctly, run my Spring Boot server? I directly have source code there, I don't even need compiling and stuffs.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • You need to point JAVA_HOME to your JRE in /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java or /usr/lib/jvm/java-11-openjdk-amd64/bin/java. Seem like in usr/bin/java/bin/java don't have Java executable file. – Dattq2303 Jul 04 '21 at 09:59
  • For example, set `JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64` and then `PATH=${PATH}:${JAVA_HOME}/bin`. – Matt Jul 04 '21 at 10:02
  • Does this answer your question? [JAVA\_HOME should point to a JDK not a JRE](https://stackoverflow.com/questions/43496192/java-home-should-point-to-a-jdk-not-a-jre) – Matt Jul 04 '21 at 10:06

1 Answers1

4

JAVA_HOME sould point to the base installation dir of java: /usr/lib/jvm/java-11-openjdk-amd64/ not /usr/bin/java which probably is a symbolic link to /usr/lib/jvm/java-11-openjdk-amd64/bin/java

then in PATH variable you append $JAVA_HOME/bin

So it should look something like this:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/
PATH=$JAVA_HOME/bin:$PATH

FYI update-alternatives is only responsible for changing the symlink of java to point it to different versions of the executable, think of it as a simple ln -s /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/bin/java

SaleemKhair
  • 499
  • 3
  • 12