Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - 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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you should do is to do what the Warning messages say. Recompile that class with the -Xlint:deprecation option. The compiler will then tell you what deprecated API you are using or overriding.

How to do that?


Once you have identified the API that is causing the problem, there are two approaches to "resolving" the error.

  1. 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 need to work out a way to replace your code's use of the deprecated element with something better.

  2. 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 be removed in a future release, and that will prevent your code from running when you upgrade. (You would be advised to review the products policy on removal of deprecated APIs.)

    • The deprecated API may have fundamental flaws that make your application unreliable in some circumstances.


(For this particular example, my guess is that the OP was using one of the deprecated methods in the Thread class:

  • countStackFrames()
  • destroy()
  • pause()
  • resume()
  • stop()
  • stop(Throwable)
  • suspend()

These methods are either unreliable, unsafe or both, and you are strongly advised not to use them. Read this explanation: "Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?".)


Someone noted in a comment that they got Deprecation warnings because they had two different versions of a library on the classpath. They "fixed it" by getting rid of one of them. In fact, in that scenario the real cause would probably have been that the newer version of the library has deprecated some API classes or methods that their application was compiled against. Their "fix" was in effect rolling back to the older version of the API.

Arguably, that's a bad thing. Their application is now stuck depending on the older version of the API, at least until they can figure out what the real cause for the problem was. The trouble is that they are building up technical debt that will eventually need to be resolved one way or another.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...