15

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

Naman
  • 27,789
  • 26
  • 218
  • 353
Davide C
  • 830
  • 2
  • 11
  • 21

4 Answers4

11

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:

[UPDATE: fixed formatting of the yaml file]

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.

igor
  • 699
  • 1
  • 9
  • 26
  • @igor Below commands didn't work. I want to install OpenJdk11 Open J9 JVM. `container_commands: 100-remove-old-java: command: "sudo yum remove -y java-1.8.0-openjdk-headless" 200-download-rpm-package: command: "cat < /etc/yum.repos.d/adoptopenjdk.repo [AdoptOpenJDK] name=AdoptOpenJDK baseurl= enabled=1 gpgcheck=1 gpgkey=https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public EOF" 300-install-java: command: "sudo yum install jdk-11.0.5+10_openj9-0.17.0"` – jay Nov 14 '19 at 12:38
  • Hey @jay. I didn't test this approach for JVMs other than AWS Corretto. If you need only that specific version of JDK then it would be better to create a separated question with error message and other details. You can leave a link to that question here. I'll see if I can help. – igor Nov 15 '19 at 08:32
  • @igor I had created the question yesterday. This is the link- https://stackoverflow.com/questions/58856955/installing-openjdk11-openj9-on-aws-beanstalk . I have another idea, to ssh into the server and then install the jdk 11 on it. – jay Nov 15 '19 at 08:42
  • @jay can you just use tested script? – igor Nov 15 '19 at 10:00
  • I get invalid key errors when I copy the yaml in the answer. I'm still trying to figure out what I should use. – Andrew Bucknell May 03 '20 at 13:26
  • @AndrewBucknell, can you post a stacktrace or exact error you get? The layout of your project/config also might be helpful. – igor May 04 '20 at 12:50
  • I have raised this question https://stackoverflow.com/questions/61575850/what-are-the-rules-for-valid-keys-in-ebextensions-files – Andrew Bucknell May 05 '20 at 00:02
10

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

Lance
  • 768
  • 7
  • 21
5

As of May 2020, Corretto 11 running on 64bit Amazon Linux 2 is now a managed platform in Elastic Beanstalk. Here is a reference to the Java SE platforms available:

https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.javase

Phil J
  • 51
  • 1
  • 1
1

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

Sofo Gial
  • 697
  • 1
  • 9
  • 20