Deprecated api error in java

[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.

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.

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(); >> >

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

Читайте также:  Php if equals zero

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:

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);
BufferedReader disObj = new BufferedReader(new InputStreamReader(bisObj));

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

Источник

Fix the Warning: Uses or Overrides a Deprecated API in Java

Fix the Warning: Uses or Overrides a Deprecated API in Java

Today, we will see why a warning says uses or overrides a deprecated API and demonstrate how to fix this to accomplish the task.

Fix Warning Saying uses or overrides a deprecated API in Java

Example Code (that contains a warning):

//import libraries  import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException;  //Main class  public class Main    //main method  public static void main(String[] args)  //path of a text file  File filePath = new File("Files/TestFile.txt");  try   //obtain input bytes from a file  FileInputStream fileInputStream = new FileInputStream(filePath);  //adds the functionality to another input stream  BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);  //lets an app read primitive Java data types from the specified input stream  DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);   if (dataInputStream.available() != 0)   // Get a line.  String line = dataInputStream.readLine();  // Place words to an array which are split by a "space".  String[] stringParts = line.split(" ");  // Initialize the word's maximum length.  int maximumLength = 1;   // iterate over each stingPart, the next one is addressed as "stringX"  for (String stringX : stringParts)   // If a document contains the word longer than.  if (maximumLength  stringX.length())  // Set the new value for the maximum length.  maximumLength = stringX.length();  >//end for-loop   // +1 because array index starts from "0".  int[] counter = new int[maximumLength + 1];  for (String str : stringParts)   // Add one to the number of words that length has  counter[str.length()] ++;  >  // We are using this kind of loop because we require the "length".  for (int i = 1; i  counter.length; i++)   System.out.println(i + " letter words: " + counter[i]);  >//end for-loop  >//end if statement  >//end try  catch (IOException ex)  ex.printStackTrace();  >//end catch  >//end main method  >//end Main class 

In this code, we access a .txt file, read that file line by line and place the words in an array which are split based on a single space . Then, we count the number of characters in each word and display all of them in the program output.

Though this program generates the output, it also highlights that we are using or overriding a deprecated API at line String line = dataInputStream.readLine(); . See the following.

This warning is being generated using the readLine() method of the DataInputStream class. According to the documentation, this method has been deprecated since JDK 1.1 because it does not convert bytes to characters properly.

Although, the method is deprecated and likely will perform as expected in some cases. But we cannot guarantee that it will fulfill its job anymore.

Therefore, it is good to use a similar but consistent method.

As of JDK 1.1 , the method preferred for reading the lines of text is the readLine() function from the BufferedReader class. We don’t have to change all the code from scratch but only need to convert the DataInputStream to the BufferedReader class.

Replace this line of code:

DataInputStream dataInputStream = new DataInputStream(in); 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); 

Now, the complete working program will look as follows.

//import libraries  import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader;  //Main class  public class Main    //main method  public static void main(String[] args)  //path of a text file  File filePath = new File("Files/TestFile.txt");  try   //obtain input bytes from a file  FileInputStream fileInputStream = new FileInputStream(filePath);  //adds the functionality to another input stream  BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);  //lets an app read primitive Java data types from the specified input stream  //DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);  BufferedReader bufferedReader = new BufferedReader(  new InputStreamReader(bufferedInputStream));   String line="";  //get a line and check if it is not null  if ((line = bufferedReader. readLine()) != null)   // Place words to an array which are split by a "space".  String[] stringParts = line.split(" ");  // Initialize the word's maximum length.  int maximumLength = 1;   // iterate over each stingPart, the next one is addressed as "stringX"  for (String stringX : stringParts)   // If a document contains the word longer than.  if (maximumLength  stringX.length())  // Set the new value for the maximum length.  maximumLength = stringX.length();  >//end for-loop   // +1 because array index starts from "0".  int[] counter = new int[maximumLength + 1];  for (String str : stringParts)   // Add one to the number of words that length has  counter[str.length()] ++;  >  // We are using this kind of loop because we require the "length".  for (int i = 1; i  counter.length; i++)   System.out.println(i + " letter words: " + counter[i]);  >//end for-loop  >//end if statement  >//end try  catch (IOException ex)  ex.printStackTrace();  >//end catch  >//end main method  >//end Main class 

Additionally, if you also see something similar to the following.

Recompile with -Xlint: deprecation for details 

Don’t worry; it just tells you an option to use while compiling to have more details about where you are using the deprecated stuff.

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

Related Article - Java Warning

Источник

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

What is warning message: uses or overrides a deprecated api

You will get this warning when you are using deprecated api in your code.

Example:
Let’s say you have simple swing code which displays a JFrame with JButton on it.

When you will compile the code, you will get below output:

C:\Users\Arpit\Desktop\javaPrograms>javac JFrameDeprecatedAPIExample.java
Note: JFrameDeprecatedAPIExample.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

As you can see, compile gives us warning message that JFrameDeprecatedAPIExample.java uses or overrides a deprecated API.

We are getting this warning message because JFrame’s show() method is deprecated and should not be used.

If you want to know about deprecated API, you can use below command:

C:\Users\Arpit\Desktop\javaPrograms>javac JFrameDeprecatedAPIExample.java -Xlint:deprecation
JFrameDeprecatedAPIExample.java:18: warning: [deprecation] show() in Window has been deprecated
frame.show();
^
1 warning

💡 Did you know?

If you are using IDEs like eclipse or intellij, then it will show deprecated APIs with strike through.

If you are using maven, you can use -Dmaven.compiler.showDeprecation=true with maven goals.

Источник

Оцените статью