How-to articles, tricks, and solutions about JAVA

Create a Maven project in Eclipse complains "Could not resolve archetype"

There could be several reasons why you are getting the error "Could not resolve archetype" when trying to create a Maven project in Eclipse.

How should I have explained the difference between an Interface and an Abstract class?

An interface is a collection of abstract methods that define a set of functions that a class must implement.

Java: Get first item from a collection

To get the first item from a collection in Java, you can use the iterator() method to get an iterator for the collection, and then call the next() method on the iterator to get the first element.

Calculate date/time difference in java

To calculate the difference between two dates in Java, you can use the java.time package (part of Java 8 and later) or the java.util.Calendar class (part of the older java.util package).

How to write logs in text file when using java.util.logging.Logger

To write logs to a text file using java.util.logging.Logger, you will need to do the following:

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.

Java format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

To convert a date in the format yyyy-MM-dd'T'HH:mm:ss.SSSz to the format yyyy-mm-dd HH:mm:ss, you can use the SimpleDateFormat class in Java.

Good examples using java.util.logging

Here are some examples of how to use the java.util.logging package to log messages in Java:

Error: Could not find or load main class in intelliJ IDE

If you receive the error "Could not find or load main class" in IntelliJ IDEA, it means that the Java Virtual Machine (JVM) cannot find the class with the main method that you are trying to run.

Whitespace Matching Regex - Java

In Java, you can use the following regular expression to match any amount of whitespace:

Java 8 Iterable.forEach() vs foreach loop

In Java 8, the Iterable.forEach() method is a default method that allows you to iterate over the elements of an Iterable (such as a List or Set) and perform a specific action on each element.

How to read XML using XPath in Java

To read XML using XPath in Java, you can use the javax.xml.xpath package.

How to get the path of src/test/resources directory in JUnit?

In JUnit, you can use the getClass().getClassLoader().getResource("").getPath() method to get the path of the src/test/resources directory.

Java Security: Illegal key size or default parameters?

If you receive the error "Illegal key size or default parameters" in Java, it means that you are trying to use a cryptographic algorithm with a key size that is not allowed by the jurisdiction policy files.

How to set specific Java version to Maven?

To set a specific Java version for Maven, you can specify the maven.compiler.source and maven.compiler.target properties in the pom.xml file.

doGet and doPost in Servlets

doGet and doPost are methods of the javax.servlet.http.HttpServlet class that are used to handle HTTP GET and POST requests, respectively.

Maven plugins can not be found in IntelliJ

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

How do I fix a NoSuchMethodError?

A NoSuchMethodError is thrown when a program tries to call a method that does not exist in the class or interface.

What is SuppressWarnings ("unchecked") in Java?

@SuppressWarnings("unchecked") is an annotation in Java that tells the compiler to suppress specific warnings that are generated during the compilation of the code.

How to clone ArrayList and also clone its contents?

To clone an ArrayList and also clone its contents, you can use the clone method of the ArrayList class, which creates a shallow copy of the list.

How can I generate random number in specific range in Android?

To generate a random number in a specific range in Android, you can use the nextInt method of the java.util.Random class.

Java default constructor

In Java, a default constructor is a constructor that is automatically generated by the compiler if no other constructors are defined in a class.

How should I escape strings in JSON?

In JSON, certain characters must be escaped in strings.

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.

Easy way to write contents of a Java InputStream to an OutputStream

One way to write the contents of a Java InputStream to an OutputStream is to use the read and write methods of the InputStream and OutputStream classes.