简体   繁体   English

如何使用 -Xlint:unchecked 进行编译?

[英]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 ?如何使用-Xlint:unchecked重新编译?

Specify it on the command line for javac:在 javac 的命令行中指定它:

javac -Xlint:unchecked javac -Xlint:未选中

Or if you are using Ant modify your javac target或者,如果您使用 Ant 修改您的 javac 目标

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

If you are using Maven, configure this in the maven-compiler-plugin如果您使用的是 Maven,请在maven-compiler-plugin

<compilerArgument>-Xlint:unchecked</compilerArgument>

对于IntelliJ 13.1 ,转到File -> Settings -> Project Settings -> Compiler -> Java Compiler ,并在右侧,对于Additional command line parameters输入"-Xlint:unchecked"

In gradle project, You can added this compile parameter in the following way:在 gradle 项目中,您可以通过以下方式添加此编译参数:

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

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.在 MyGui.java 中的某个地方,您使用了一个没有指定类型的泛型集合。 For example if you're using an ArrayList somewhere, you are doing this:例如,如果您在某处使用 ArrayList,您将执行以下操作:

List list = new ArrayList();

When you should be doing this:当你应该这样做时:

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

There is another way for gradle: gradle还有另一种方法:

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

For Android Studio add the following to your top-level build.gradle file within the allprojects block对于 Android Studio,将以下内容添加到allprojects块中的顶级build.gradle文件中

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

In CMD, write:在CMD中,写:

javac -Xlint:unchecked MyGui2.java

it will display the list of unchecked or unsafe operations.它将显示未检查或不安全操作的列表。

If you work with an IDE like NetBeans, you can specify the Xlint:unchecked compiler option in the propertys of your project.如果您使用 NetBeans 等 IDE,则可以在项目的属性中指定Xlint:unchecked编译器选项。

Just go to projects window, right click in the project and then click in Properties .只需转到项目窗口,右键单击项目,然后单击Properties

In the window that appears search the Compiling category, and in the textbox labeled Additional Compiler Options set the Xlint:unchecked option.在出现的窗口中搜索Compiling类别,并在标记为Additional Compiler Options的文本框中设置Xlint:unchecked选项。

Thus, the setting will remain set for every time you compile the project.因此,每次编译项目时,该设置都将保持设置。

other way to compile using -Xlint:unchecked through command line使用 -Xlint:unchecked 通过命令行编译的其他方法

javac abc.java -Xlint:unchecked

it will show the unchecked and unsafe warnings.它将显示未经检查和不安全的警告。

指定 Gradle 编译器参数的更简洁方法如下:

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

FYI getting to these settings has changed for IntelliJ 15, new and improved for even deeper burial!仅供参考,IntelliJ 15 的这些设置已更改,新的和改进的用于更深的埋藏!

Now it's: File > Other Settings > Default Settings > 'Build, Execution, Deployment' > Compiler > Java Compiler现在是:文件 > 其他设置 > 默认设置 > '构建、执行、部署' > 编译器 > Java 编译器

And in Additional command line parameters, same as before, write "-Xlint:unchecked".在附加命令行参数中,与以前一样,写“-Xlint:unchecked”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM