Reputation: 1055
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.
Upvotes: 26
Views: 95710
Reputation: 719229
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?
If you are compiling from a command shell using javac
just add the -Xlint:deprecation
option to the command line.
For Maven builds, run maven as follows:
mvn clean install -Dmaven.compiler.showDeprecation=true
For Gradle builds, add the following to the root build file:
allprojects {
tasks.withType(JavaCompile) {
options.deprecation = true
}
}
or set the option on specific compile tasks.
For Ant builds, set the deprecation
attribute to true
in the javac
task.
For IDEs:
Once you have identified the API that is causing the problem, there are three approaches to "resolving" the error.
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.
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.
Injudicious suppression of these warnings is just creating technical dept that you or your successors will have to resolve in the future.
If the deprecation warning was due to a change in a Java SE itself or in a 3rd-party API that you are using, you could "resolve" it by rolling back to the version that didn't show the warnings. This is even a worse idea than the previous one. By rolling back, you are just allowing the technical dept to accumulate.
(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?".)
Upvotes: 33
Reputation: 33273
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.
Step 2. Read the documentation for the deprecated API to learn how to replace it.
Upvotes: 4