How do I compile with -Xlint:unchecked?
Asked Answered
C

12

95

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?

Capo answered 21/11, 2011 at 17:19 Comment(3)
Are you compiling on the command line? If so, add the -Xlint:unchecked to the command line you're executing, just as the message indicates.Seasick
depends on how you run your code.. is it from eclipse? command line?Dependency
What did you do? I have never seen this error message...Generally
L
58

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>
Lilllie answered 21/11, 2011 at 17:20 Comment(2)
I have maven 3, model 4.0 -- The <compilerArgument> goes under <configuration> not within the `<compilerArguments>' element. Silly me.Amann
And what about compile inside AOSP? What should I add to Android.mk file?Counterman
C
54

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".

Clarino answered 12/6, 2014 at 5:4 Comment(4)
Thanks. But for 13.1 Community Edition it's at File - Settings - Compiler - Java Compiler, and the field is at the bottom.Romeliaromelle
For 14.0.3 this settings page has moved to: File - Settings - Build, Execution, Deployment - Compiler - Java Compiler.Kenna
Not on AndroidStudio, there Compiler options seem to be delivered for any compiler, not only Java. Editing build.gradle does work.Sarto
In Android Studio the compiler options can be found under File -> New Project Settings -> Settings for New Projects. They however seem to not be applicable to an existing project.Feudality
F
37

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

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked"
    }
}
Fino answered 7/1, 2014 at 23:9 Comment(3)
In recent gradle versions the task type Compile has been renamed to JavaCompile, so it is tasks.withType(JavaCompile) { ... }Wimsatt
I would add that this should go in allprojects { // ... }Oruntha
Out of all other options, this is the only that worked for me. Thank you so muchNiple
C
23

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>();
Carpenter answered 21/11, 2011 at 17:22 Comment(2)
Thanks a lot,this helped me!Afoul
This doesn't even attempt to answer the question given.Eidetic
Y
18

There is another way for gradle:

compileJava {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
Yoruba answered 5/9, 2014 at 7:55 Comment(3)
This won't affect the tests for example.Johnnyjohnnycake
For Android Studio, this seems to require a bit different approach: tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" }Sarto
for Android Studio 2.1.3 add this to project build.gradle allprojects { repositories { jcenter() } gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" } } }Yoruba
K
6

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" 
}
Kowal answered 9/5, 2019 at 6:29 Comment(0)
I
3

In CMD, write:

javac -Xlint:unchecked MyGui2.java

it will display the list of unchecked or unsafe operations.

Infusionism answered 18/7, 2012 at 19:9 Comment(0)
U
3

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.

Upu answered 21/11, 2013 at 13:30 Comment(0)
F
2

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

javac abc.java -Xlint:unchecked

it will show the unchecked and unsafe warnings.

Fleecy answered 16/10, 2013 at 11:53 Comment(0)
D
2

A cleaner way to specify the Gradle compiler arguments follow:

compileJava.options.compilerArgs = ['-Xlint:unchecked','-Xlint:deprecation']
Duran answered 2/1, 2017 at 0:25 Comment(1)
This may be shorter but it’s not cleaner. For one thing, it overrides existing options set elsewhere. For another, it won’t work affect test, as noted in the comments below tarn’s answer. Don’t do this, use xianlinbox’s answer instead.Tarrant
C
1

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'
            }
        }

    // ...
}
Cutpurse answered 13/2 at 7:30 Comment(0)
J
0

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".

Juxtapose answered 2/6, 2016 at 9:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.