0

I have created a maven web project in eclipse. I want to compile it in jdk 1.6 and have its run time environment as 1.8.I have tomcat as the web container to run the project. For compiling I am aware of the Compliance level option in the project settings. Is it the correct way to go about the compile time requirement? And what settings do I have to change to make it run on java 1.8?

ghostrider
  • 2,046
  • 3
  • 23
  • 46

1 Answers1

1

Use the Maven compiler plugin to set the source and runtime version of jdk & jvm respectively.

<build>
 <pluginManagement>
  <plugins>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
      <source>1.6</source>
      <target>1.8</target>
      </configuration>
   </plugin>
  <plugins>
<pluginManagement>
<build>
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • Are you sure? Because source in your answer is version of your code and target is compile time you want your code to be able to compiled in. I guess this is for compiling only and not for running. Please correct me if I am wrong? – ghostrider Aug 25 '18 at 18:37
  • 1
    Specifying the target ensures that the compiler will output .class files for that version of jvm. Specifying the source makes the compiler accept code for that version of jdk, for e.g. if source was set to 1.6 and you had lambda expression in your code you would get issues during compilation. – Fullstack Guy Aug 25 '18 at 18:42
  • thanks for the explanation. I came across https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html article which says there is no guarantee this will work in the note section at the bottom. What's your take on it? – ghostrider Aug 26 '18 at 05:30
  • 1
    Code compiled in JDK 1.6 should run in a 1.8 JVM, as they are backward compatible. But there are a few edge cases as described by this Oracle document: https://www.oracle.com/technetwork/java/javase/compatibility-417013.html#incompatibilities – Fullstack Guy Aug 26 '18 at 09:11
  • Cant we achieve this using the Compliance level setting in Eclipse? And with the Installed JRE combination ? – ghostrider Aug 26 '18 at 10:09
  • 1
    If the project is imported as a Maven project, specifying the `source` and `target` in the Maven compiler plugin will do the trick. The m2eclipse plugin will take the compliance level from the pom.xml and override the settings configured in eclipse. I suggest you take a look at this answer also for better clarity. https://stackoverflow.com/a/3539194/3888766 – Fullstack Guy Aug 26 '18 at 12:30