Search code examples
spring-bootjava-8shamazon-linuxamazon-linux-2

problem with Java 8 in Amazon linux 1 vs Amazon linux 2


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

Solution

  • 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.