I created a Spring Boot application using Gradle. I wrote about the procedure to deploy this to AWS EC2.
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 will be created in ./build/libs/
The name of the jar file 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 ec2-user@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   
$ ssh -i AWS key path ec2-user@IPv4 public IP
Check if the file has been transferred
$ ls
WebToDoStarter-0.0.1-SNAPSHOT.jar
There is
Try installing java8
$ 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 | 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 | 
$ 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
$ sudo yum install -y java-11-amazon-corretto.x86_64
You can't just install it
$ sudo alternatives --config java
There are 2 programs'java'To provide.
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 select the current[+]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)
Now you can do it
Tomcat, which is a servlet container for executing Java Servlet, does not need to be installed this time. This is because it is deployed with a jar file that can be executed by the built-in Tomcat.
$ java -jar WebToDoStarter-0.0.1-SNAPSHOT.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)
application.yml
server:
  port: 8081
Add
Recommended Posts