How-to articles, tricks, and solutions about JAVA

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.

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:

Short form for Java if statement

In Java, you can use the ternary operator (also known as the conditional operator) to create a short form for an if-else statement.

Simple HTTP server in Java using only Java SE API

To create a simple HTTP server in Java using only the Java SE API, you can use the java.net package.

Simple way to count character occurrences in a string

To count the occurrences of a character in a string in Java, you can use the charAt() method of the String class to iterate over the characters in the string and check if each character is equal to the target character.

Simple way to repeat a string

In Java, you can use the String.repeat() method to repeat a string multiple times.

Simplest way to read JSON from a URL in Java

To read JSON from a URL in Java, you can use the readJsonFromUrl() method from the JSON simple library.

Solving a "communications link failure" with JDBC and MySQL

A "communications link failure" error when using JDBC and MySQL can be caused by several factors. Here are a few potential solutions:

Sort an array in Java

To sort an array in Java, you can use the Arrays.sort() method of the java.util.Arrays class. This method sorts the elements of the array in ascending order according to their natural ordering.

Sort ArrayList of custom Objects by property

To sort an ArrayList of custom objects by a property, you can use the Collections.sort method and pass it a custom Comparator.

Sorting HashMap by values

To sort a HashMap by values in Java, you can use the Map.Entry interface and the Comparator interface to create a comparator that compares the values in the map.

Split Java String by New Line

To split a string in Java by new line, you can use the split method of the String class and pass it the regular expression "\n" or "\r\n", depending on the platform you are running on.

Spring Boot - Cannot determine embedded database driver class for database type NONE

If you are seeing the error "Cannot determine embedded database driver class for database type NONE" in a Spring Boot application, it means that the application is trying to auto-configure an embedded database, but it is unable to determine which database

Spring Boot - Error creating bean with name 'dataSource' defined in class path resource

The error "Error creating bean with name 'dataSource' defined in class path resource" in Spring Boot typically indicates that there is a problem with the configuration of the dataSource bean.

Spring Boot - How to log all requests and responses with exceptions in single place?

In a Spring Boot application, you can log all requests and responses, along with exceptions, in a single place by using a combination of Spring Boot's logging framework and request/response interceptors.

Spring Boot and multiple external configuration files

To use multiple external configuration files with Spring Boot, you can use the spring.config.name property to specify the names of the configuration files.

Spring cron expression for every day 1:01:am

To create a cron expression that triggers an event every day at 1:01 AM, you can use the following cron expression:

Spring Data JPA - "No Property Found for Type" Exception

This exception is usually thrown when you are trying to access a property that does not exist in the entity class you are querying.

Spring JPA selecting specific columns

To select specific columns using Spring JPA, you can use the @Query annotation and specify the columns in the SELECT clause of the JPQL query.

Spring MVC - How to return simple String as JSON in Rest Controller

To return a simple string as JSON in a Spring MVC Rest Controller, you can use the @ResponseBody annotation and return the string directly.

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.

SSL and cert keystore

An SSL (Secure Sockets Layer) keystore is a storage location for SSL certificates, which are used to establish secure, encrypted connections between a client and a server. The keystore is typically managed by a keystore manager, such as the Java Keytool,

stale element reference: element is not attached to the page document

The "stale element reference" error in Selenium WebDriver occurs when an element that was previously found on the webpage is no longer attached to the DOM (Document Object Model) and is therefore no longer accessible through the browser.

Static Classes In Java

In Java, a static class is a class that can be accessed without an instance of the class. A static class is defined by adding the static keyword to the class declaration.

String concatenation: concat() vs "+" operator

In Java, you can use the concat() method of the java.lang.String class or the + operator to concatenate (join) two or more strings.