2

Im trying out the "Foreign Function & Memory API (Preview)" in Java 19. Ive used jextract to create netcdf_h.java. This is working from a main() program in src, but when I try to run it from a test program in test, it fails with the message:

Preview features are not enabled for sunya/cdm/netcdf/ffm/netcdf_h (class file version 63.65535). Try running with '--enable-preview' java.lang.UnsupportedClassVersionError: Preview features are not enabled for sunya/cdm/netcdf/ffm/netcdf_h (class file version 63.65535). Try running with '--enable-preview'

Im using gradle to build:

...

tasks {
    val ENABLE_PREVIEW = "--enable-preview"
    withType<JavaCompile>() {
        options.compilerArgs.add(ENABLE_PREVIEW)
        // Optionally we can show which preview feature we use.
        options.compilerArgs.add("-Xlint:preview")
        // Explicitly setting compiler option --release
        // is needed when we wouldn't set the
        // sourceCompatiblity and targetCompatibility
        // properties of the Java plugin extension.
        options.release.set(19)
    }
    withType<Test>() {
        useJUnitPlatform()
        jvmArgs!!.add(ENABLE_PREVIEW)
    }
    withType<JavaExec>() {
        jvmArgs!!.add(ENABLE_PREVIEW)
    }
    withType<KotlinCompile> {
        kotlinOptions.jvmTarget = "19"
    }
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(19))
    }
}

And IntelliJ to run.

Seems like maybe theres somewhere else I need to add "--enable-preview" in gradle for tests ??

John Caron
  • 1,367
  • 1
  • 10
  • 15
  • By the way, the latest version is 3rd preview in Java 21: [*JEP 442: Foreign Function & Memory API (Third Preview)*](https://openjdk.org/jeps/442) – Basil Bourque Aug 22 '23 at 21:26

1 Answers1

1

Thanks to this answer:

should be:

withType<Test>() {
    useJUnitPlatform()
    jvmArgs("--enable-preview")
}
withType<JavaExec>() {
    jvmArgs("--enable-preview")
}
John Caron
  • 1,367
  • 1
  • 10
  • 15