DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building AI Applications With Java and Gradle
  • How To Add Three Photo Filters to Your Applications in Java
  • How To Convert HTML to PNG in Java
  • Deploying a Kotlin App to Heroku

Trending

  • Nobody Designs an RBAC Mess; Everyone Ends Up With One
  • Going All In On the JVM 25 Virtual Threads
  • The Twelve-Factor Agents: Building Production-Ready LLM Applications
  • Data Integration Landscape 2026: Event Streaming, API, and Batch in the Era of Agentic AI

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. Gradle Goodness: Enabling Preview Features For Java

Gradle Goodness: Enabling Preview Features For Java

Check out how to enable and play with preview Java features in Gradle.

By 
Hubert Klein Ikkink
·
Mar. 05, 21 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Java introduced preview features in the language starting in Java 12. These features can be tried out by developers, but are still subject to change and can even be removed in the next release. By default, the preview features are not enabled when we want to compile and run our Java code. We must explicitly specify that we want to use the preview feature to the Java compiler and Java runtime using the command-line argument --enable-preview. In Gradle, we can customize our build file to enable preview features. We must customize tasks of type JavaCompile and pass --enable-preview to the compiler arguments. Also tasks of type Test and JavaExec must be customized where we need to add the JVM argument --enable-preview.

In the following Gradle build script written in Kotlin, we have a Java project written with Java 15 where we reconfigure the tasks to enable preview features:

Kotlin
 




xxxxxxxxxx
1
48


 
1
plugins {
2
    java
3
    application
4
}
5
6
repositories {
7
    mavenCentral()
8
}
9
10
dependencies {
11
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
12
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
13
}
14
15
application {
16
    mainClass.set("mrhaki.Patterns")
17
}
18
19
tasks {
20
    val ENABLE_PREVIEW = "--enable-preview"
21
22
    // In our project we have the tasks compileJava and
23
    // compileTestJava that need to have the
24
    // --enable-preview compiler arguments.
25
    withType<JavaCompile>() {
26
        options.compilerArgs.add(ENABLE_PREVIEW)
27
28
        // Optionally we can show which preview feature we use.
29
        options.compilerArgs.add("-Xlint:preview")
30
31
        // Explicitly setting compiler option --release
32
        // is needed when we wouldn't set the
33
        // sourceCompatiblity and targetCompatibility
34
        // properties of the Java plugin extension.
35
        options.release.set(15)
36
    }
37
38
    // Test tasks need to have the JVM argument --enable-preview.
39
    withType<Test>() {
40
        useJUnitPlatform()
41
        jvmArgs.add(ENABLE_PREVIEW)
42
    }
43
44
    // JavaExec tasks need to have the JVM argument --enable-preview.
45
    withType<JavaExec>() {
46
        jvmArgs.add(ENABLE_PREVIEW)
47
    }
48
}



Written with Gradle 6.8.3

Java (programming language) Gradle

Published at DZone with permission of Hubert Klein Ikkink. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building AI Applications With Java and Gradle
  • How To Add Three Photo Filters to Your Applications in Java
  • How To Convert HTML to PNG in Java
  • Deploying a Kotlin App to Heroku

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook