How-to articles, tricks, and solutions about JAVA

How do I find out what keystore my JVM is using?

To find out which keystore the Java Virtual Machine (JVM) is using, you can use the javax.net.ssl.KeyManagerFactory class and the getDefaultAlgorithm method.

How to pretty print XML from Java?

To pretty print XML from Java, you can use the javax.xml.transform.Transformer class from the Java XML Transformer API.

Using Pairs or 2-tuples in Java

In Java, you can use the javafx.util.Pair class to represent a pair or 2-tuple.

Does Java have support for multiline strings?

In Java, there is no native support for multiline strings.

Base64 Java encode and decode a string

To encode and decode a string in Base64 in Java, you can use the java.util.Base64 class.

How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

To determine whether you are running in a 64-bit JVM or a 32-bit JVM from within a Java program, you can use the sun.arch.data.model system property.

Getting the array length of a 2D array in Java

To get the length of a 2D array in Java, you can use the length field of the array.

How can I convert a stack trace to a string?

To convert a stack trace to a string in Java, you can use the printStackTrace() method of the Throwable class, which prints the stack trace to a PrintWriter.

MySQL JDBC Driver 5.1.33 - Time Zone Issue

If you are using MySQL JDBC Driver version 5.1.33 and you are experiencing issues with time zones, there are a few things you can try:

How do I update an entity using spring-data-jpa?

To update an entity using Spring Data JPA, you will need to follow these steps:

How can I convert List<Integer> to int[] in Java?

You can use the toArray() method of the List interface to convert a List<Integer> to an int[] in Java.

How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?

To import the javax.servlet or jakarta.servlet API in an Eclipse project, follow these steps:

IntelliJ inspection gives "Cannot resolve symbol" but still compiles code

There are a few possible reasons why IntelliJ might show a "Cannot resolve symbol" error while still being able to compile your code.

How do you create a dictionary in Java?

To create a dictionary (or map) in Java, you can use the Map interface and its implementing classes.

Java 8 List<V> into Map<K, V>

To convert a List<V> into a Map<K, V> in Java 8, you can use the toMap() method of the Collectors class from the java.util.stream package.

Java - get the current class name?

To get the current class name in Java, you can use the getClass() method of the Object class, which returns a Class object that represents the runtime class of the object.

How can I download and save a file from the Internet using Java?

To download and save a file from the Internet using Java, you can use the URL and URLConnection classes from the java.net package.

Connect Java to a MySQL database

To connect a Java application to a MySQL database, you need to use the JDBC (Java Database Connectivity) API.

How to connect to Oracle using Service Name instead of SID

To connect to an Oracle database using a service name instead of a SID (System Identifier), you can use the thin driver and specify the connection URL in the following format:

How can I create a memory leak in Java?

It is generally not a good idea to intentionally create a memory leak in Java.

How to create a directory in Java?

To create a directory in Java, you can use the mkdir() method of the File class.

How do I get the last character of a string?

To get the last character of a string in Java, you can use the charAt() method of the String class, which returns the character at a specific index in the string.

How to convert an Array to a Set in Java

To convert an array to a Set in Java, you can use the Arrays.asList() method to create a List from the array, and then use the List.toSet() method to create a Set from the List.

How can I sort Map values by key in Java?

To sort the values of a Map by key in Java, you can use the TreeMap class, which is a Map implementation that maintains its entries in ascending key order.