Java 代码编译期间遇到警告消息“使用或覆盖已弃用的 API”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18311964/
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
Warning message "uses or overrides a deprecated API" encountered during code compilation
提问by Lawrance
I compiled my program and I got the following error. How should I resolve it?
我编译了我的程序,但出现以下错误。我该如何解决?
Note: ClientThreadClients.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
采纳答案by Stephen C
What you should do is to do what the Warning messages say. Recompile that class with the -Xlint:deprecation
option.
您应该做的是按照警告消息所说的去做。使用-Xlint:deprecation
选项重新编译该类。
The compiler will then tell you what deprecated API you are using or overriding.
然后编译器会告诉您正在使用或覆盖哪些已弃用的 API。
(Alternatively, if you showed us the source code of this class, we may be able to spot the problem for you ... or compile it ourselves.)
(或者,如果您向我们展示了该类的源代码,我们或许可以为您发现问题……或者自己编译。)
But I'm going to guessthat you are using one of the deprecated methods in the Thread
class:
但我猜你正在使用Thread
类中不推荐使用的方法之一:
countStackFrames()
destroy()
pause()
resume()
stop()
stop(Throwable)
suspend()
countStackFrames()
destroy()
pause()
resume()
stop()
stop(Throwable)
suspend()
These methods are either unreliable, unsafe or both. You are strongly advised not to use them. Read this explanation: "Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?".
这些方法要么不可靠,要么不安全,或者两者兼而有之。强烈建议您不要使用它们。阅读此解释:“为什么不推荐使用 Thread.stop、Thread.suspend 和 Thread.resume?”。
Once you have identified the API that is causing the problem, there are two approaches to "resolving" the error.
一旦确定了导致问题的 API,有两种方法可以“解决”错误。
You can read the javadocs for the deprecated API to find out whyit is deprecated. Then based on what the javadocs say, and the context, you work out a way to replace your code's use of the deprecated element with something better.
You can use the
@SuppressWarnings("deprecation")
annotation to tell the compiler to "be quiet" about it. This is generally a bad idea:- The deprecated API may beremoved in a future release.
- The deprecated API mayhave fundamental flaws that make your application unreliable in some circumstances.
您可以阅读已弃用 API 的 javadoc 以了解它为何被弃用。然后根据 javadoc 所说的内容和上下文,您可以找到一种方法来用更好的东西替换代码对已弃用元素的使用。
您可以使用
@SuppressWarnings("deprecation")
注释告诉编译器“保持安静”。这通常是一个坏主意:- 不推荐使用的 API可能会在未来版本中删除。
- 已弃用的 API可能存在一些根本性缺陷,使您的应用程序在某些情况下不可靠。
回答by Klas Lindb?ck
Step 1: Find out which deprecated API the code is using. If you use a modern IDE (eclipse or similar tool), the deprecated code will be clearly marked, usually with a strikethrough font. If you compile from the command prompt add -Xlint:deprecation
to the command line when you compile.
第 1 步:找出代码使用的是哪个已弃用的 API。如果您使用现代 IDE(eclipse 或类似工具),已弃用的代码将被清楚地标记出来,通常带有删除线字体。如果从命令提示符编译,则-Xlint:deprecation
在编译时添加到命令行。
Step 2. Read the documentation for the deprecated API to learn how to replace it.
步骤 2. 阅读已弃用 API 的文档以了解如何替换它。