94

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?

Etienne Perot
  • 4,764
  • 7
  • 40
  • 50
Dave Fisher
  • 1,033
  • 3
  • 10
  • 10
  • 1
    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. – 101100 Nov 21 '11 at 17:21
  • depends on how you run your code.. is it from eclipse? command line? – Boaz Nov 21 '11 at 17:21
  • What did you do? I have never seen this error message... – AlexR Nov 21 '11 at 17:22

11 Answers11

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>
guerda
  • 23,388
  • 27
  • 97
  • 146
ewan.chalmers
  • 16,145
  • 43
  • 60
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".

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Brian Burns
  • 20,575
  • 8
  • 83
  • 77
  • Thanks. But for 13.1 Community Edition it's at File - Settings - Compiler - Java Compiler, and the field is at the bottom. – RenniePet Sep 22 '14 at 22:42
  • 8
    For 14.0.3 this settings page has moved to: File - Settings - Build, Execution, Deployment - Compiler - Java Compiler. – csauve Sep 29 '15 at 19:50
  • 1
    Not on AndroidStudio, there Compiler options seem to be delivered for *any* compiler, not only Java. Editing **build.gradle** does work. – Alex Cohn Mar 26 '16 at 09:54
  • 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. – JJD Aug 16 '20 at 12:09
36

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

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked"
    }
}
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
xianlinbox
  • 969
  • 10
  • 10
24

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>();
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
18

There is another way for gradle:

compileJava {
    options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
Phantom
  • 1,704
  • 4
  • 17
  • 32
tarn
  • 546
  • 6
  • 10
  • This won't affect the tests for example. – NoDataDumpNoContribution Jun 14 '15 at 21:41
  • 2
    For Android Studio, this seems to require a bit different approach: `tasks.withType(JavaCompile) { options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" }` – Alex Cohn Mar 26 '16 at 09:55
  • 3
    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" } } }` – tarn Sep 02 '16 at 07:40
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" 
}
Wirling
  • 4,810
  • 3
  • 48
  • 78
3

In CMD, write:

javac -Xlint:unchecked MyGui2.java

it will display the list of unchecked or unsafe operations.

Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36
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.

Josue
  • 77
  • 7
2

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

javac abc.java -Xlint:unchecked

it will show the unchecked and unsafe warnings.

JJD
  • 50,076
  • 60
  • 203
  • 339
2

A cleaner way to specify the Gradle compiler arguments follow:

compileJava.options.compilerArgs = ['-Xlint:unchecked','-Xlint:deprecation']
JJD
  • 50,076
  • 60
  • 203
  • 339
Jim H
  • 106
  • 1
  • 3
  • 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. – Konrad Rudolph Sep 04 '18 at 10:29
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".

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
HAKitz
  • 1