I'm new to Spring Boot and I'm trying to develop an application to later deploy it on AWS beanstalk. I started the project using java 11 but later I discovered that AWS only support java 8. Is it possible to set 'maven.compiler.target' in pom.xml to 1.8 to make it running correctly? Should I have to use Java 1.8 for both development and compile? I would like to use new Java features and library. I would like to have some opinion if someone have same problems. Thanks. Cd
问题:
回答1:
You can install java 11 on your instances using ebextensions. Just create a folder .ebextensions in your source bundle and add there one file with following name 10_java.config and content:
container_commands:
100-remove-old-java:
command: "sudo yum remove -y java-1.8.0-openjdk-headless"
200-download-rpm-package:
command: "wget https://d3pxv6yz143wms.cloudfront.net/11.0.4.11.1/java-11-amazon-corretto-devel-11.0.4.11-1.x86_64.rpm "
300-install-java:
command: "sudo yum localinstall -y java-11-amazon-corretto-devel-11.0.4.11-1.x86_64.rpm"
This will remove default java 8 and install AWS' distribution of java 11.
回答2:
Since you're using Java 11 why not take advantage of Java and Elastic Bean Stalks docker support and create a docker image with JDK11 and use this to deploy?
If you choose not to go down this path and you want to want to target a lower version of java to use elastic beanstalk with Java 8 you can try something like this.
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
[...]
</build>
[...]
</project>
Merely setting the target option does not guarantee that your code actually runs on a JRE with the specified version. The pitfall is unintended usage of APIs that only exist in later JREs which would make your code fail at runtime with a linkage error. To avoid this issue, you can either configure the compiler's boot classpath to match the target JRE or use the Animal Sniffer Maven Plugin to verify your code doesn't use unintended APIs. In the same way, setting the source option does not guarantee that your code actually compiles on a JDK with the specified version. To compile your code with a specific JDK version, different than the one used to launch
Keep in mind though that if you compile and run your code on Java 8, you cannot use classes that have been added to Java's standard library in Java 11, because those will not be present on Java 8. link
While searching, I found that support for OpenJDK11 may be coming. we re-affirm that the OpenJDK 8 and OpenJDK 11 Java runtimes in Amazon Linux 2 will continue to receive free long-term support from Amazon until at least June 30, 2023
Link
回答3:
When you compile a Java project with a specific Java version, you can only run it with a version that is greater (or equal) than the one you used to compile it. The opposite cannot be done, at least not if you are using features of the language that are present in later versions.
E.g. you cannot use features from Java 11 but to run the application in Java 8