Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning message "uses or overrides a deprecated API" encountered during code compilation

Tags:

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. 
like image 302
Lawrance Avatar asked Aug 19 '13 10:08

Lawrance


Video Answer


2 Answers

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:

    • Eclipse: see Eclipse not showing deprecated warning?
    • NetBeans: see Where can I find the Xlint option in Netbeans?
    • Intellij: see Intellij Idea find all deprecated usages in the project

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.

like image 177
Stephen C Avatar answered Nov 03 '22 01:11

Stephen C


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.

like image 23
Klas Lindbäck Avatar answered Nov 03 '22 02:11

Klas Lindbäck