How-to articles, tricks, and solutions about JAVA

How to POST form data with Spring RestTemplate?

To POST form data with the RestTemplate class in Spring, you can use the postForObject method and pass it a URL, an object containing the form data, and the response type.

How to parse JSON in Kotlin?

To parse JSON in Kotlin, you can use the JSONObject class from the org.json package.

How to sort a HashMap in Java

To sort a HashMap in Java, you can use the TreeMap class, which is a Map implementation that maintains its entries in ascending order, sorted according to the keys.

How to hash some String with SHA-256 in Java?

To hash a string using SHA-256 in Java, you can use the MessageDigest class from the java.security package.

How to subtract X day from a Date object in Java?

To subtract a certain number of days from a Date object in Java, you can use the Calendar class.

Android SDK installation doesn't find JDK

If the Android SDK installation is unable to find the JDK (Java Development Kit), it could be because the JDK is not installed or is not installed in the default location.

What is the meaning of the CascadeType.ALL for a @ManyToOne JPA association

In a JPA entity relationship, the CascadeType.ALL annotation specifies that all operations (persist, merge, remove, refresh, and detach) that are performed on the parent entity should be cascaded to the child entity.

How to solve could not create the virtual machine error of Java Virtual Machine Launcher?

If you are getting the "Could not create the Java virtual machine" error when trying to launch a Java program, it means that the Java Virtual Machine (JVM) is unable to allocate enough memory to run the program.

Eclipse/Java code completion not working

If code completion is not working in Eclipse, there are a few possible reasons:

How do I programmatically determine operating system in Java?

To programmatically determine the operating system in Java, you can use the System.getProperty method and pass it the "os.name" property.

Java - How to create new Entry (key, value)

To create a new key-value pair in a Map in Java, you can use the put method.

What does %5B and %5D in POST requests stand for?

The strings "%5B" and "%5D" are URL-encoded versions of the square brackets "[" and "]", respectively.

How to execute a java .class from the command line

To execute a Java class from the command line, you will need to use the java command and specify the name of the class that you want to run.

Getting the name of the currently executing method

To get the name of the currently executing method in Java, you can use the Thread.currentThread().getStackTrace() method.

eclipse won't start - no java virtual machine was found

There are a few possible reasons why Eclipse might not be able to find a Java Virtual Machine (JVM).

What is the difference between JSF, Servlet and JSP?

JavaServer Faces (JSF) is a user interface (UI) framework for building web applications.

&& (AND) and || (OR) in IF statements

In Java, the && and || operators are used in if statements to combine multiple conditions.

Can't execute jar- file: "no main manifest attribute"

The "no main manifest attribute" error is usually caused by a missing or incorrect Main-Class attribute in the manifest file of a Java .jar file.

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

The java.lang.UnsupportedClassVersionError error is usually caused by trying to run a Java class that was compiled with a newer version of the Java compiler than the version of the JRE (Java Runtime Environment) that you are using.

How do I get a platform-dependent new line character?

To get a platform-dependent new line character in Java, you can use the System.lineSeparator() method.

What exactly is a Maven Snapshot and why do we need it?

A Maven snapshot is a version of a Maven artifact that is under active development and has not been released yet.

How do I convert 2018-04-10T04:00:00.000Z string to DateTime?

To convert a string in the format "2018-04-10T04:00:00.000Z" to a DateTime object in Java, you can use the org.joda.time.DateTime class and the DateTimeFormatter class.

The import javax.servlet can't be resolved

If you are getting the error "The import javax.servlet can't be resolved", it means that the javax.servlet package is not available on the classpath.

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.

How to create a temporary directory/folder in Java?

To create a temporary directory/folder in Java, you can use the createTempDirectory() method of the Files class in the java.nio.file package.