javagradleincompatibility

Class file for XYZ uses preview features of Java SE 17 Error


I'm trying to make a scheduled Whatsapp sender and thought of using Auties00 WhatsApp4J library. After creating a new project and adding the code to create a connection, following error occures:

error: class file for .gradle/caches/modules-2/files-2.1/com.github.auties00/whatsappweb4j/3.3.1/d8c9b37af703062b20b137100d8dc11abe8a27f7/whatsappweb4j-3.3.1.jar(/it/auties/whatsapp/api/Whatsapp.class) uses preview features of Java SE 17.
  (use --enable-preview to allow loading of class files which contain preview features)
1 error

I'm currently using JDK 19 with Gradle 7.6.1 so I'm confused why it's throwing the afore mentioned error.

I tried to upgrade from the TLS JDK 17 to JDK19 but the error persists. Adding the --enable-preview flag didn't work either. This is Auties00 WhatsApp4J library

My build.gradle looks like this:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.0.6'
    id 'io.spring.dependency-management' version '1.1.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '19'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'com.github.auties00:whatsappweb4j:3.3.1'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

And my main class like this:

package com.example.demo;

import it.auties.whatsapp.api.Whatsapp;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Whatsapp.webBuilder()
                .newConnection()
                .build()
                .addLoggedInListener(() -> System.out.println("Connected"))
                .addDisconnectedListener(reason -> System.out.printf("Disconnected: %s%n", reason))
                .connect()
                .join();
    }
}


Solution

  • I managed to compile the project. Apparently are preview features not back compatible within Java. Thanks to @user16320675's help I found this on Gradle Docs:

    tasks.withType(JavaCompile).configureEach {
        options.compilerArgs += "--enable-preview"
    }
    
    tasks.withType(Test).configureEach {
        jvmArgs += "--enable-preview"
    }
    
    tasks.withType(JavaExec).configureEach {
        jvmArgs += "--enable-preview"
    }
    

    I added the snipped to my build.gradle file and had to change my projects jdk from 19 to 17, because only with version 17 the preview features I needed were available.