[Solved] java uses or overrides a deprecated api. recompile with -xlint:deprecation for details.

In this post, I will be sharing about warning message java uses or overrides a deprecated api. recompile with -Xlint:deprecation for details. First, we will understand when do we get this warning, then we will produce this warning before moving on to the solution.

Read Also: Count Number Of Pages in PDF in Java

When do we get java uses or overrides a deprecated api warning

As the name suggests you are getting this warning message because you are using deprecated api in your code.

 ðŸ’¡ Did You Know?

If you are using IDEs like Eclipse, IntelliJ, etc. then you will find that the deprecated APIs are shown with strike-through.

Producing the warning: java uses or overrides a deprecated api


We can easily produce the java uses or overrides a deprecated api warning. For example, in the below code we are using DataInputStream class readLine() method which was marked as deprecated and should not be used.
 import java.io.*;
public class WarningDeprecatedApi {
    public static void main(String args[]) {
        try 
        {    
            String str = null;            
            FileInputStream fisObj = new FileInputStream(new File("C:\\Users\\SUBMITTAL\\Documents\\Sample.txt"));
            BufferedInputStream bisObj = new BufferedInputStream(fisObj);
            DataInputStream disObj = new DataInputStream(bisObj);
            while ((str = disObj.readLine()) != null)
            {
                System.out.println(str);
            }
            disObj.close();
            bisObj.close();
            fisObj.close();
        }
        catch(IOException ex) 
        {
            ex.printStackTrace();
        }
    }
}

Output:

C:\Users\SUBMITTAL\Desktop\docs>javac WarningDeprecatedApi.java
Note: WarningDeprecatedApi.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

What you should do


Append -Xlint:deprecation to the javac YOURCLASSNAME.java command to know more about the deprecated API as shown below in the example:

C:\Users\SUBMITTAL\Desktop\docs>javac WarningDeprecatedApi.java -Xlint:deprecation

WarningDeprecatedApi.java:10: warning: [deprecation] readLine() in DataInputStream has been deprecated
                        while ((str = disObj.readLine()) != null)
                                                       ^
1 warning

For Maven, you can use -Dmaven.compiler.showDeprecation=true with maven goals:

mvn clean install -Dmaven.compiler.showDeprecation=true

Solution:

You can get rid of this warning message in two ways:

1. Replace DataInputStream's readLine() method with BufferedReader class's readLine() method.

To get rid of the warning message and run the java program, just replace the below line:
 DataInputStream disObj = new DataInputStream(bisObj);


with

 BufferedReader disObj = new BufferedReader(new InputStreamReader(bisObj));


in the above code.

2. Adding @SuppressWarnings("deprecation") annotation to the main method [Not Recommended]

Add the @SuppressWarnings("deprecation") annotation to the main method as shown below in the above code:

 @SuppressWarnings("deprecation")
public static void main(String args[]) {


It is considered a bad practice to add @SuppressWarnings("deprecation") annotation to tell the compiler to "be quiet" about it because in the future the deprecated API may be removed and that will prevent your code from running when you upgrade.

If you implement any of the two solutions then you will no longer be receiving the warning message.

That's all for today. Please mention in the comments in case you have any questions related to warning java uses or overrides a deprecated api. recompile with -Xlint:deprecation for details.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry