How-to articles, tricks, and solutions about JAVA

How to decompile a whole Jar file?

To decompile a JAR file, you can use a Java decompiler such as JD-GUI.

Get an OutputStream into a String

To get an OutputStream into a String, you can use a ByteArrayOutputStream as the OutputStream and then use the toString() method of the ByteArrayOutputStream to get the contents of the stream as a string.

How do I convert from int to Long in Java?

To convert an int to a long in Java, you can simply use the typecast operator (long) and place it in front of the value you want to convert.

Java: How to get input from System.console()

To get input from the System.console() in Java, you can use the readLine() method of the Console class.

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

The "failed to lazily initialize a collection of role" exception in Hibernate is thrown when you try to access an uninitialized collection from a Hibernate entity when the entity is in a detached state.

org.xml.sax.SAXParseException: Content is not allowed in prolog

The error org.xml.sax.SAXParseException: Content is not allowed in prolog usually indicates that there is some content before the XML prolog in the file you are trying to parse.

How to change font size in Eclipse for Java text editors?

To change the font size in the Java text editor in Eclipse, follow these steps:

How to use java.String.format in Scala?

In Scala, you can use the format method of the String class to format strings in a way similar to the printf function in C.

Calculating days between two dates with Java

To calculate the number of days between two dates in Java, you can use the Period class from the java.time package introduced in Java 8.

Java recursive Fibonacci sequence

To implement a recursive Fibonacci sequence in Java, you can define a recursive method that returns the nth number in the sequence.

How to get milliseconds from LocalDateTime in Java 8

To get the milliseconds from a LocalDateTime object in Java 8, you can use the toInstant method and the toEpochMilli method.

Java ResultSet how to check if there are any results

To check if a ResultSet object contains any results in Java, you can use the next method of the ResultSet class.

Random shuffling of an array

To shuffle an array randomly in Java, you can use the Collections.shuffle method.

What is a daemon thread in Java?

In Java, a daemon thread is a thread that runs in the background and does not prevent the program from exiting.

Display current time in 12 hour format with AM/PM

To display the current time in 12-hour format with AM/PM in Java, you can use the SimpleDateFormat class and the Calendar class.

How to convert the following json string to java object?

To convert a JSON string to a Java object, you can use the fromJson method of the Gson class.

Append a single character to a string or char array in java?

To append a single character to a string or char array in Java, you can use the + operator or the concat method for strings, or you can use the Arrays.copyOf method for char arrays.

What do 3 dots next to a parameter type mean in Java?

In Java, the three dots (...) next to a parameter type indicate that the parameter is a varargs parameter.

Java - What does "\n" mean?

In Java, the string "\n" is a newline character.

Gradle build without tests

To build a Gradle project without running the tests, you can use the assemble task instead of the build task.

Downloading Java JDK on Linux via wget is shown license page instead

If you are trying to download the Java Development Kit (JDK) on Linux using wget and you are being shown the license page instead of the JDK download, it is likely because the download page has changed since the wget command was written.

Calling remove in foreach loop in Java

If you want to remove elements from a collection while iterating over it using a for-each loop in Java, you must use an iterator instead of the looping construct.

How to break out or exit a method in Java?

To break out or exit a method in Java, you can use the return statement.

How to get values and keys from HashMap?

To get the values and keys from a HashMap in Java, you can use the values and keySet methods.

How can I sort a List alphabetically?

To sort a List alphabetically in Java, you can use the Collections.sort method and pass in your List as an argument.