How-to articles, tricks, and solutions about JAVA

Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use

If you get an error saying that several ports required by the Tomcat Server are already in use, it means that another program is using one or more of the ports that Tomcat is trying to bind to. Tomcat typically uses the following ports:

Setting the default Java character encoding

To set the default character encoding for the Java Virtual Machine (JVM), you can use the -Dfile.encoding option when starting the JVM.

How to nicely format floating numbers to string without unnecessary decimal 0's

To format a floating-point number as a string without unnecessary decimal zeros, you can use the DecimalFormat class from the java.text package. The DecimalFormat class allows you to specify a pattern for formatting numbers as strings.

Make copy of an array

To make a copy of an array in Java, you can use the clone() method of the Object class. The clone() method creates a shallow copy of the array, which means that it creates a new array with the same elements as the original array

What is the difference between JDK and JRE?

The Java Development Kit (JDK) is a software development kit that contains the tools and libraries needed to develop Java applications. The JDK includes the Java Runtime Environment (JRE), which is a set of libraries and tools that allow Java applications

How to convert float to int with Java

To convert a float to an int in Java, you can use the (int) type cast operator. The (int) operator will truncate the decimal part of the float value and convert it to an int.

Http 415 Unsupported Media type error with JSON

A HTTP 415 Unsupported Media Type error means that the server is unable to process the request because the request entity has a media type that the server does not support. In the context of a JSON request, this means that the server is unable to process

What is a StackOverflowError?

A StackOverflowError is an error that occurs when the Java Virtual Machine (JVM) runs out of space on the call stack. The call stack is a data structure that is used to store information about method calls, including the name of the method, the parameters

How to read json file into java with simple JSON library

To read a JSON file into Java using the Simple JSON library, you can use the JSONObject class and the JSONArray class.

How to cast an Object to an int

To cast an Object to an int in Java, you can use the intValue() method of the Integer class.

How to access a value defined in the application.properties file in Spring Boot

To access a value defined in the application.properties file in Spring Boot, you can use the @Value annotation and the Environment interface.

How to clear the console?

To clear the console in most command-line interfaces (CLI), you can use the clear or cls command.

How to call a method after a delay in Android

To call a method after a delay in Android, you can use the Handler class and the postDelayed() method. The postDelayed() method takes a Runnable and a delay in milliseconds as arguments, and it runs the Runnable after the specified delay.

How to pass a function as a parameter in Java?

In Java, you can pass a function as a parameter using a functional interface. A functional interface is an interface that has a single abstract method. You can create a functional interface by annotating an interface with the @FunctionalInterface annotati

How to filter a Java Collection (based on predicate)?

To filter a Java collection based on a predicate, you can use the removeIf method of the Collection interface. This method removes all elements from the collection that match the specified predicate.

How to implement a tree data-structure in Java?

A tree is a data structure that consists of nodes arranged in a hierarchy. Each node has a value and may have zero or more child nodes. The top node in a tree is called the root node.

How to run a JAR file

To run a JAR file, you need to have Java installed on your computer. If you don't have it installed, you can download it from the Oracle website.

Converting ISO 8601-compliant String to java.util.Date

To convert an ISO 8601-compliant string to a java.util.Date object, you can use the java.text.SimpleDateFormat class and specify the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format, which is the ISO 8601 format for dates with a time and time zone.

How to find Java Heap Size and Memory Used (Linux)?

To find the Java heap size and the amount of memory used on a Linux system, you can use the jstat command and specify the process ID (PID) of the Java process.

How to add local .jar file dependency to build.gradle file?

To add a local JAR file dependency to a build.gradle file, you can use the compile fileTree() method and specify the directory where the JAR file is located.

How do I get the file extension of a file in Java?

To get the file extension of a file in Java, you can use the File class and its getName() method to get the file name, and then use the substring() method of the String class to extract the extension from the file name.

Mockito : how to verify method was called on an object created within a method?

To verify that a method was called on an object created within a method using Mockito, you can use the Mockito.verify() method and pass it the object that you want to verify, as well as the method that you want to verify was called.

How can I parse/format dates with LocalDateTime? (Java 8)

In Java 8 and later, you can use the java.time.LocalDateTime class to represent a date and time without a time zone. To parse a date and time string into a LocalDateTime object, you can use the java.time.format.DateTimeFormatter class.

How to build JARs from IntelliJ properly?

To build a JAR (Java Archive) file from IntelliJ IDEA, you need to follow these steps:

Remove all occurrences of char from string

To remove all occurrences of a particular character from a string, you can use the replace() method. This method takes two arguments: the string to be replaced, and the string to replace it with. If you pass an empty string as the second argument