如何抑制有关已弃用 api 的 javac 警告?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1554538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:03:05  来源:igfitidea点击:

How can I suppress javac warnings about deprecated api?

javacompiler-warningsjavac

提问by IttayD

When I compile, javac outputs:

当我编译时,javac 输出:

Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.`

I wish to suppress this warning. Trying -Xlint:none does not seem to help.

我想压制这个警告。尝试 -Xlint:none 似乎没有帮助。

采纳答案by Thomas Owens

From what I can tell in the docs, you can't do it on the command-line.

从我在文档中可以看出,您不能在命令行上执行此操作。

According to the javacdocumentation, -Xlint:none only disables warnings "not mandated by the Java Language Specification". It appears that warning you of the use of deprecated APIs is managed by the language spec.

根据javac文档, -Xlint:none 仅禁用“Java 语言规范未强制要求”的警告。似乎警告您使用已弃用的 API 是由语言规范管理的。

Your best option would be to fix the use of deprecated APIs. However, an option would be to add the @SuppressWarnings("deprecation")annotation to the classes or methods that are using the deprecated APIs.

您最好的选择是修复已弃用的 API 的使用。但是,一个选项是将@SuppressWarnings("deprecation")注释添加到使用已弃用 API 的类或方法。

回答by Joachim Sauer

Two possible ways:

两种可能的方式:

  1. don't use deprecated API
  2. Use @SuppressWarnings("deprecation")
  1. 不要使用已弃用的 API
  2. @SuppressWarnings("deprecation")

回答by Matt Poush

If it's a core Java API, there is almost certainly a replacement that will do what you want. Run the javac with that extra parameter, and then look at the API for the deprecated method and replace as appropriate.

如果它是一个核心 Java API,那么几乎可以肯定有一个替代品可以满足您的需求。使用该额外参数运行 javac,然后查看已弃用方法的 API 并根据需要进行替换。

回答by Jeff

For others that were Google searching this problem and stumble upon this thread like I did...

对于谷歌搜索这个问题并像我一样偶然发现这个线程的其他人......

Try: -Xlint:-deprecation

尝试:-Xlint:-弃用

It seems to work on JDK 6... not sure about others.

它似乎适用于 JDK 6 ......不确定其他人。

回答by Wolfgang

With Java 6, neither the @Depreated annotation, nor a comiler flag will help you here. The only solution that worked for me was to put a javadoc commentwith the @deprecated(small caps) tag on the deprecated method:

在 Java 6 中,@Depreated 注释和编译器标志都无济于事。唯一对我有用的解决方案是在已弃用的方法上添加带有@deprecated(小型大写字母)标记的javadoc 注释

/**
  * @deprecated overriding deprecated method
  */
@Override
public javax.xml.bind.Validator createValidator() throws JAXBException {...}

(The example is from a class that derives from JAXBContext.)

(该示例来自从 JAXBContext 派生的类。)

(I didn't import the deprecated Validator class to avoid a warning on the import statement.)

(我没有导入已弃用的 Validator 类以避免在导入语句中出现警告。)

回答by coolcoder

use nowarn attribute see below

使用 nowarn 属性见下文

e.g.

例如

<javac srcdir="src" 
  destdir="build/classes" source="1.6" 
  target="1.6" debug="true" encoding="Cp1252"
  nowarn="on">

by default nowarn attribute is off

默认情况下,nowarn 属性关闭

回答by Arne Burmeister

When using gradleyou can configure it easily:

使用gradle 时,您可以轻松配置它:

tasks.withType(JavaCompile) {
    options.deprecation = false
}

(tested with Gradle 2 and Java 8)

(使用 Gradle 2 和 Java 8 测试)

回答by Kaushal28

@SuppressWarnings("deprecation")is not working for me, instead I've used

@SuppressWarnings("deprecation")对我不起作用,而是我用过

@SuppressWarnings("unchecked")

回答by mbd

If you are compiling in the command line you can filter the messages with grep, just filtering out the messages that has unwanted content, like for example grep -v deprecated. You can use |to send the output to grep,

如果您在命令行中进行编译,则可以使用 过滤消息grep,只需过滤掉包含不需要内容的消息,例如grep -v deprecated. 您可以使用|将输出发送到 grep,

your compile command | grep -v deprecated