How-to articles, tricks, and solutions about JAVA

Why is my Spring @Autowired field null?

There are several reasons why an @Autowired field in a Spring bean might be null:

Java Array Sort descending?

To sort an array in descending order in Java, you can use the Arrays.sort method and pass it a comparator that compares the elements in reverse order.

How to set a Timer in Java?

To set a timer in Java, you can use the java.util.Timer class.

Access restriction on class due to restriction on required library rt.jar?

If you are getting an "access restriction" error on a class in your Java code, it means that you are trying to access a class or member (field or method) that has restricted access.

How to remove line breaks from a file in Java?

There are several ways to remove line breaks from a file in Java.

UTF-8 byte[] to String

To convert a byte array to a string in UTF-8 encoding, you can use the following method in Java:

Popup Message boxes

A pop-up message box is a small window that appears on a computer screen to display a message or prompt the user to take a specific action.

java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy

To convert the format of a java.util.Date object from yyyy-MM-dd to MM-dd-yyyy, you can use the SimpleDateFormat class and its format() method.

How to convert int[] into List<Integer> in Java?

To convert an int[] array into a List<Integer> in Java, you can use the Arrays.stream() method to create a stream of the array, and then use the mapToObj() method to map each element of the stream to an Integer object.

Spring RestTemplate GET with parameters

To perform a GET request with parameters using the RestTemplate in Spring, you can use the getForObject() method and pass it a URL with placeholders for the parameters, as well as a map of the parameter values.

Tomcat: How to find out running Tomcat version?

There are several ways to find out the version of Tomcat that is running on your system:

Printing HashMap In Java

To print the contents of a HashMap in Java, you can use the entrySet() method to get a set of key-value pairs and then iterate over the set to print the keys and values.

Generating all permutations of a given string

To generate all permutations of a given string in Java, you can use a recursive approach.

How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer?

You can download the latest Java Runtime Environment (JRE) or Java Development Kit (JDK) as a zip file from the official website of Oracle (https://www.oracle.com/java/technologies/javase-downloads.html).

How to read file from relative path in Java project? java.io.File cannot find the path specified

To read a file from a relative path in a Java project, you can use the File class from the java.io package and specify the relative path to the file.

Difference between FetchType LAZY and EAGER in Java Persistence API?

In the Java Persistence API (JPA), the FetchType enum is used to specify the strategy for fetching data from the database.

Way to get number of digits in an int?

To get the number of digits in an int in Java, you can use the log10() method of the Math class and then add 1 to the result.

How to solve javax.net.ssl.SSLHandshakeException Error?

The javax.net.ssl.SSLHandshakeException error is usually caused by a problem with the SSL/TLS certificate of the server you are trying to connect to.

How do I tell Maven to use the latest version of a dependency?

To tell Maven to use the latest version of a dependency, you can use the [RELEASE] or [LATEST] version range in the <version> element of the dependency in your pom.xml file.

How can I pad a String in Java?

To pad a string in Java, you can use the String.format() method and specify a width for the string.

How to add one day to a date?

There are a few different ways to add one day to a date.

How to print to the console in Android Studio?

To print to the console in Android Studio, you can use the Log class from the android.util package.

How to use Class<T> in Java?

In Java, the Class<T> class represents the class or interface type of a class at runtime.

How to convert ASCII code (0-255) to its corresponding character?

To convert an ASCII code (0-255) to its corresponding character in Java, you can use the char data type and cast the ASCII code to it.

Using scanner.nextLine()

Scanner.nextLine() is a method of the Scanner class in Java that reads a line of text from the input.