How-to articles, tricks, and solutions about JAVA

Java: parse int value from a char

To parse an integer value from a char in Java, you can use the Character.getNumericValue() method.

java.io.FileNotFoundException: the system cannot find the file specified

The java.io.FileNotFoundException: the system cannot find the file specified error is usually thrown when a program tries to access a file that does not exist, or that it does not have permission to access.

java.net.ConnectException: Connection refused

The java.net.ConnectException: Connection refused exception is thrown when an application tries to connect to a remote host, but the connection is refused by the host. This can happen for several reasons, such as:

java.net.SocketException: Connection reset

A java.net.SocketException: Connection reset is a runtime exception that is thrown when a connection is reset. This can be caused by a variety of issues, such as:

java.net.SocketTimeoutException: Read timed out under Tomcat

A java.net.SocketTimeoutException: Read timed out error can occur when a connection to a server is blocked by a firewall or if the server is experiencing high load and is unable to process requests in a timely manner.

java.net.UnknownHostException: Invalid hostname for server: local

The java.net.UnknownHostException: Invalid hostname for server: local exception usually indicates that a hostname or an IP address could not be resolved. This can happen for a variety of reasons, such as:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

A java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) error typically indicates that the MySQL server is unable to authenticate the user with the provided username and password.

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.

java.util.Date to XMLGregorianCalendar

To convert a java.util.Date object to an XMLGregorianCalendar in Java, you can use the toGregorianCalendar method of the DatatypeFactory class.

javac error: Class names are only accepted if annotation processing is explicitly requested

This error message indicates that you are trying to use a class name in a source code file that is not being processed by the Java compiler.

javac is not recognized as an internal or external command, operable program or batch file [closed]

The error javac is not recognized as an internal or external command, operable program or batch file occurs when you try to run the javac command from the command prompt, but the command prompt cannot find the javac executable.

javac not working in windows command prompt

If javac is not working in the Windows command prompt, there could be several reasons for this.

JAX-RS — How to return JSON and HTTP status code together?

In JAX-RS, you can use the Response class from the javax.ws.rs.core package to return a JSON response and an HTTP status code together. Here's an example of how to do this:

JPA JoinColumn vs mappedBy

In a JPA entity mapping, the @JoinColumn annotation is used to specify the foreign key column for a many-to-one or one-to-one relationship.

JUnit 5: How to assert an exception is thrown?

To read a text file in Java, you can use the BufferedReader class from the java.io package.

Key existence check in HashMap

To check if a key exists in a HashMap in Java, you can use the containsKey method.

keytool error Keystore was tampered with, or password was incorrect

The "Keystore was tampered with, or password was incorrect" error typically occurs when you try to use the keytool utility to access a keystore with an incorrect password or when the keystore has been modified in some way.

Left padding a String with Zeros

To left pad a String with zeros in Java, you can use the String.format() method and the %0Nd format specifier, where N is the total length of the padded string.

Main differences between SOAP and RESTful web services in Java

SOAP (Simple Object Access Protocol) and REST (Representational State Transfer) are two different styles of web services that can be used to expose the functionality of a web-based system over the internet.

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

Making a mocked method return an argument that was passed to it

To make a mocked method return an argument that was passed to it, you can use the Mockito.when method and pass it the argument that you want to return as the answer.

Math.random() explanation

Math.random() is a method in the java.lang.Math class that returns a random double value between 0.0 (inclusive) and 1.0 (exclusive).

Maven package/install without test (skip tests)

To package/install a Maven project without running the tests, you can use the maven-install-plugin and specify the skipTests property as true.

Maven plugins can not be found in IntelliJ

There are several possible reasons why Maven plugins might not be found in IntelliJ:

max value of integer

In Java, the maximum value of an int type is 2147483647. This is the highest positive number that can be represented with a 32-bit binary number.