警告消息“使用或覆盖已弃用的API".代码编译过程中遇到的问题 [英] Warning message "uses or overrides a deprecated API" encountered during code compilation
问题描述
我编译了程序,并收到以下错误.我应该如何解决?
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.
推荐答案
您应该做的是执行警告消息中所述的内容.用-Xlint:deprecation
选项重新编译该类.
What you should do is to do what the Warning messages say. Recompile that class with the -Xlint:deprecation
option.
然后,编译器将告诉您正在使用或覆盖的不推荐使用的API.
The compiler will then tell you what deprecated API you are using or overriding.
(或者,如果您向我们展示了此类的源代码,我们也许可以为您发现问题……或自行编译.)
(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 guess that you are using one of the deprecated methods in the Thread
class:
-
countStackFrames()
-
destroy()
-
pause()
-
resume()
-
stop()
-
stop(Throwable)
-
suspend()
countStackFrames()
destroy()
pause()
resume()
stop()
stop(Throwable)
suspend()
这些方法要么不可靠,要么不安全,或者两者兼而有之.强烈建议您不要使用它们.阅读以下说明:为什么是Thread.stop,线程.暂停并弃用Thread.resume?".
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?".
一旦确定了导致问题的API,就有两种方法可以解决"该错误.
Once you have identified the API that is causing the problem, there are two approaches to "resolving" the error.
-
您可以阅读不赞成使用的API的javadocs,以了解为什么不赞成使用的API.然后根据javadocs的说法和上下文,找到一种更好的方法来替换代码对不赞成使用的元素的使用.
You can read the javadocs for the deprecated API to find out why it 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.
您可以使用@SuppressWarnings("deprecation")
批注告诉编译器保持安静".这通常是个坏主意:
You can use the @SuppressWarnings("deprecation")
annotation to tell the compiler to "be quiet" about it. This is generally a bad idea:
- 已弃用的API 可能会在将来的版本中删除.
- 不推荐使用的API 可能具有一些基本缺陷,在某些情况下会使您的应用程序不可靠.
- The deprecated API may be removed in a future release.
- The deprecated API may have fundamental flaws that make your application unreliable in some circumstances.
这篇关于警告消息“使用或覆盖已弃用的API".代码编译过程中遇到的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!