[Java] Deploy SpringBoot application to AWS EC2

2 minute read

I created a Spring Boot application using Gradle. I wrote the procedure to deploy this to AWS EC2.

Create executable jar

Create an executable jar with gradle’s bootJar task Reference: https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plugin/reference/html/#packaging-executable-and-normal

build.gradle


bootJar {
    launchScript()
}

Then on the command line

$ gradle bootJar

When executed, a jar file is created in ./build/libs/

Transfer this jar file to EC2

The jar file name created this time is WebToDoStarter-0.0.1-SNAPSHOT.jar, and it is assumed that it is placed on Desktop

Transfer this file to EC2 using sftp

$ sftp -i AWS key path [email protected] IPv4 public IP

sftp> put Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar
Uploading Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar to /home/ec2-user/WebToDoStarter-0.0.1-SNAPSHOT.jar
Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar 100% 20MB 1.2MB/s 00:17

Access EC2 with #ssh

$ ssh -i AWS key path [email protected] IPv4 public IP

Check if files are transferred

$ls
WebToDoStarter-0.0.1-SNAPSHOT.jar

There is

Install JDK

Try installing java8 for trial

$ Sudo yum install java-1.8.0-openjdk-devel.x86_64
$ java -jar WebToDoStarter-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/demo/WebToDoApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:757)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

It’s an error, apparently the version is old in java8

Class file version and JRE version

Class file version JRE version
49 Java 5
50 Java 6
51 Java 7
52 Java 8
53 Java 9
54 Java 10
55 Java 11
56 Java 12
57 Java 13
58 Java 14

Find a higher JDK than Java 8

$ sudo yum search java
=================================================== ========================== N/S matched: java ================== ================================================== =========
~abridgement~
java-1.8.0-openjdk-devel.x86_64: OpenJDK Development Environment 8
~abridgement~
java-11-amazon-corretto.x86_64: Amazon Corretto development environment

java-11-amazon-corretto can be used up to java11

java-11-amazon-corretto installation

$ sudo yum install -y java-11-amazon-corretto.x86_64

Not just installed

Switch version

$ sudo alternatives --config java

There are 2 programs that provide'java'.

Select command

*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.amzn2.0.1.x86_64/jre/bin/java)
   2 /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java

Press Enter to keep the current selection [+] or enter the selection number: 2
$ java --version
openjdk 11.0.7 2020-04-14 LTS
OpenJDK Runtime Environment Corretto-11.0.7.10.1 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.7.10.1 (build 11.0.7+10-LTS, mixed mode)

You can do this

Tomcat, a servlet container for executing Java Servlet, does not need to be installed this time. This is because it is deployed as a jar file that can be executed by embedded Tomcat.

$ java -jar WebToDoStarter-0.0.1-SNAPSHOT.jar

  .____ _ __ _ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
(( )\___ |'_ |'_| |'_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| |) )) ))
  '|____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot :: (v2.1.1.RELEASE)

By the way, to change the port number of the application

application.yml


server:
  port: 8081

Add