Dave Fisher
Dave Fisher

Reputation: 1053

How do I compile with -Xlint:unchecked?

I'm getting a message when I compile my code:

Note: H:\Project2\MyGui2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

How do I recompile with -Xlint:unchecked?

Upvotes: 96

Views: 171001

Answers (12)

Amit
Amit

Reputation: 83

The answer in 2024:

In the app-level build.gradle file add the following code and build your app again.

android {
    // ...

        tasks.withType(JavaCompile).tap {
            configureEach {
                options.compilerArgs += '-Xlint:unchecked'
            }
        }

    // ...
}

Upvotes: 1

deepak jaiswal
deepak jaiswal

Reputation: 21

other way to compile using -Xlint:unchecked through command line

javac abc.java -Xlint:unchecked

it will show the unchecked and unsafe warnings.

Upvotes: 2

Jim H
Jim H

Reputation: 106

A cleaner way to specify the Gradle compiler arguments follow:

compileJava.options.compilerArgs = ['-Xlint:unchecked','-Xlint:deprecation']

Upvotes: 2

Wirling
Wirling

Reputation: 5415

For Android Studio add the following to your top-level build.gradle file within the allprojects block

tasks.withType(JavaCompile) {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" 
}

Upvotes: 7

xianlinbox
xianlinbox

Reputation: 979

In gradle project, You can added this compile parameter in the following way:

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked"
    }
}

Upvotes: 37

HAKitz
HAKitz

Reputation: 1

FYI getting to these settings has changed for IntelliJ 15, new and improved for even deeper burial!

Now it's: File > Other Settings > Default Settings > 'Build, Execution, Deployment' > Compiler > Java Compiler

And in Additional command line parameters, same as before, write "-Xlint:unchecked".

Upvotes: 0

Brian Burns
Brian Burns

Reputation: 22070

For IntelliJ 13.1, go to File -> Settings -> Project Settings -> Compiler -> Java Compiler, and on the right-hand side, for Additional command line parameters enter "-Xlint:unchecked".

Upvotes: 54

tarn
tarn

Reputation: 556

There is another way for gradle:

compileJava {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}

Upvotes: 18

ewan.chalmers
ewan.chalmers

Reputation: 16265

Specify it on the command line for javac:

javac -Xlint:unchecked

Or if you are using Ant modify your javac target

  <javac ...>
    <compilerarg value="-Xlint"/>
  </javac> 

If you are using Maven, configure this in the maven-compiler-plugin

<compilerArgument>-Xlint:unchecked</compilerArgument>

Upvotes: 58

Josue
Josue

Reputation: 77

If you work with an IDE like NetBeans, you can specify the Xlint:unchecked compiler option in the propertys of your project.

Just go to projects window, right click in the project and then click in Properties.

In the window that appears search the Compiling category, and in the textbox labeled Additional Compiler Options set the Xlint:unchecked option.

Thus, the setting will remain set for every time you compile the project.

Upvotes: 3

Himanshu Aggarwal
Himanshu Aggarwal

Reputation: 1809

In CMD, write:

javac -Xlint:unchecked MyGui2.java

it will display the list of unchecked or unsafe operations.

Upvotes: 3

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15109

I know it sounds weird, but I'm pretty sure this is your problem:

Somewhere in MyGui.java you're using a generic collection without specifying the type. For example if you're using an ArrayList somewhere, you are doing this:

List list = new ArrayList();

When you should be doing this:

List<String> list = new ArrayList<String>();

Upvotes: 23

Related Questions