0

I'm running into a problem when migrate to run my Springboot app from Amazon linux 1 to Amazon linux 2. I'm using run file with select the Java version by JAVA_HOME:

  • Amazon linux 1: JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64
  • Amazon linux 2: JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.amzn2.0.1.x86_64/jre/bin/java

Every thing work normal in Amazon linux 1 but in Amazon linux 2, I got the Unsupported major.minor version 52.0 error. What really confuse me is that when I change the whole java version of the instance (attached image) then everything is running ok again.

I'm guessing the problem is how I point to the java jre but I can't figure it out. Can somebody please help me with this. Thanks in advance.

enter image description here

Edit 1: The sh file i use to run:

#!/bin/sh
exec 2>&1

ulimit -n 10240

#For Java Classpath
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.amzn2.0.1.x86_64/jre/bin/java
JAVA_BIN=$JAVA_HOME/bin/
export JAVA_HOME

EXTERNAL_HOME=external
EXTERNAL_RESOURCE=$EXTERNAL_HOME/resources

export SPRING_CONFIG_NAME=application
export SPRING_CONFIG_LOCATION=$EXTERNAL_RESOURCE/
export LOG_DIR=$EXTERNAL_HOME/logs
export LANG=ja_JP.UTF-8
cd $EXTERNAL_HOME
echo $SPRING_CONFIG_NAME
echo $SPRING_CONFIG_LOCATION
echo $LOG_DIR
#exec nice -n 20 java -server -Xmx512M -Xms256M -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 -jar external-0.0.1.jar
#exec nice -n 20 java -server -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -jar external-0.0.1.jar
exec nice -n 20 java -server -Xmx512M -Xms256M -Dlogging.config=file:$EXTERNAL_RESOURCE/log4j2.properties \

 -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar external-0.0.1.jar
Dattq2303
  • 302
  • 2
  • 13
  • How are you running your Spring Boot application? Via tomcat? – Jignesh M. Khatri Jun 28 '21 at 04:28
  • @JigneshM.Khatri Right now I run it by sh file using command: sudo sh run – Dattq2303 Jun 28 '21 at 04:31
  • So whatever script you are running, may be that is reading Java from wrong location. Can you provide the script which you are using to run the program. – Jignesh M. Khatri Jun 28 '21 at 04:34
  • @JigneshM.Khatri I add it, please check it – Dattq2303 Jun 28 '21 at 04:42
  • Okay I suspect few things here. Firstly does `/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.amzn2.0.1.x86_64/jre/bin/java` this path contains `/bin` directory and that `bin` directory contains Java executables? Secondly, in the `exec` command append `$JAVA_BIN` ahead of `java` so that it will read Java from correct path - something like this `exec nice -n 20 $JAVA_BIN/java -server ...`. – Jignesh M. Khatri Jun 28 '21 at 04:51
  • @JigneshM.Khatri First: Yes, it contain the Java executables file. Second: I believe the export JAVA_HOME will do the trick so no need for the $JAVA_BIN. I run this normaly on Amazon linux 1 – Dattq2303 Jun 28 '21 at 05:01
  • 1
    Reason it is working in Amazon linux 1 because it might be having only one Java installed there. In Amazon linux 2 you have multiple Java installed. And to execute `java` command, `JAVA_HOME` is not required. `java` reads from `PATH` variable. So exporting `JAVA_HOME` doesn't makes any sense as such. Check this - https://stackoverflow.com/questions/45246552/java-home-or-path-or-both/45246656 – Jignesh M. Khatri Jun 28 '21 at 05:14
  • 1
    So here what mandatory is to check what `PATH` variable is pointing to. If it is pointing to another JVM than which you require, then as I said, you need to append `path to bin` to execute that particular java. – Jignesh M. Khatri Jun 28 '21 at 05:15
  • @JigneshM.Khatri I will try like you said. Thanks for your comment – Dattq2303 Jun 28 '21 at 05:47
  • @JigneshM.Khatri You are right sir. After add the PATH then it work again. And in Amazon linux 1, my coworker had change the java version of instance into Java 8 lead to I believe that my sh run file was right. Please write an answer so I can accept it. – Dattq2303 Jun 28 '21 at 06:15

1 Answers1

2

Reason it might be working in Amazon linux 1 is it might be having only one Java installed there (or PATH is pointing to correct Java version). In Amazon linux 2 you have multiple Java installed. And to execute java command, JAVA_HOME is not required. java command reads executable from PATH variable. So exporting JAVA_HOME doesn't makes any sense as such. Check this - JAVA_HOME or PATH or BOTH?

So here what mandatory is to check what PATH variable is pointing to. If it is pointing to another JVM than which you require, then you need to append path to bin to execute that particular java, something like this - exec nice -n 20 $JAVA_HOME/bin/java -server ....

Also as per my personal opinion, there is no need to export any variable from script unless you need that variable in another script which might be executed after the one which is exporting the variable. If you want to use that variable in single script only, then just use it without exporting it.

Jignesh M. Khatri
  • 1,407
  • 1
  • 14
  • 22