5

I am using Micronaut2.0.2 application with IntelliJ IDE 2020.2.2. I have enabled the preview feature in from the language level in IDE

enter image description here

And in the Gradle file I have the below option

java {
    sourceCompatibility = JavaVersion.toVersion('14')
    targetCompatibility = JavaVersion.toVersion('14')
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
    options.compilerArgs.addAll([
            '-parameters',
            // enables incremental compilation
            '-Amicronaut.processing.incremental=true',
            '-Amicronaut.processing.annotations=fete.bird.*',
            "-Amicronaut.processing.group=$project.group",
            "-Amicronaut.processing.module=$project.name",
            "--enable-preview"
    ])
}

Getting an error as error: invalid source release 14 with --enable-preview

San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • Have you tried the answer from [here](https://stackoverflow.com/questions/54156370/errorjava-error-invalid-source-release-13-using-jdk12-with-intellij)? – Eulodos Oct 01 '20 at 17:38
  • Yes I did still same issue – San Jaisy Oct 02 '20 at 00:02
  • First - I do not understand why do you set Language Level to 15 preview for the module but use 14 in Gradle build. Second: you must not modify any build settings (source/target, dependencies etc) in IDE UI in case of the Gradle project. You should only configure it in Gradle build files. Have you modified any other build options for project? Do you use **IntelliJ IDEA** or **Gradle** for the Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | **Build and run using**. Does it work if you build project from command line using Gradle? – Andrey Oct 02 '20 at 08:32
  • I have reproduced this when I have 15 JDK set for project - which is used for compilation. When I set it to 14 JDK it works. – Andrey Oct 02 '20 at 08:38
  • When we create a Micronaut project, the java version is 14. But when we want to use the preview features, we need to add the 15 previews in the IntelliJ IDE. I haven't change any build configuration – San Jaisy Oct 02 '20 at 10:47
  • Does this answer your question? [intellij idea - Error: java: invalid source release 1.9](https://stackoverflow.com/questions/46280859/intellij-idea-error-java-invalid-source-release-1-9) – Ravi Parekh Aug 30 '21 at 15:16
  • It has nothing to do with ´preview´, This is intellij ERROR. You need to set jvm in Intellij preference, https://stackoverflow.com/questions/46280859/intellij-idea-error-java-invalid-source-release-1-9/68859868 – Ravi Parekh Aug 30 '21 at 15:19

2 Answers2

5

Set Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Gradle JVM to 14 JDK version.

Andrey
  • 15,144
  • 25
  • 91
  • 187
  • If I set to 14 version, I won't be able to use the preview feature, currently I have oracle open jdk 15 – San Jaisy Oct 02 '20 at 10:48
  • I don't have installed JDK 14. I have 15. I have the same setup for spring boot application and it working fine – San Jaisy Oct 02 '20 at 11:45
  • `If I set to 14 version, I won't be able to use the preview feature` but you set the language level to 14 in your build.gradle file. How do you want to use features from 15 then? – Andrey Oct 06 '20 at 08:59
  • In the whole world of this question in SO, this was the only answer mentioning the right thing to solve my problem. Thanks! – Ali Tou Aug 04 '21 at 10:46
3

If you have some wrong configurations in your pom.xml like this remove it. It worked for me after I remove the configs:

    <configuration>
        <source>15</source>
        <target>15</target>
        <compilerArgs>--enable-preview</compilerArgs>
    </configuration>

That's what was messing up for me. Since I already have Java 16 already.

Kidus Tekeste
  • 651
  • 2
  • 10
  • 28