How-to articles, tricks, and solutions about JAVA

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.

Eclipse - no Java (JRE) / (JDK) ... no virtual machine

If you see an error message in Eclipse saying "no Java (JRE) / (JDK) ... no virtual machine", it means that Eclipse is unable to find a valid Java installation on your system.

Eclipse "Error: Could not find or load main class"

If you are seeing an error message that says "Error: Could not find or load main class", it is likely that there is a problem with the classpath for your project.

Eclipse comment/uncomment shortcut?

In Eclipse, you can use the following keyboard shortcuts to comment and uncomment lines of code:

Eclipse error ... cannot be resolved to a type

If you are seeing an error in Eclipse that says "cannot be resolved to a type," it usually means that Eclipse is unable to find the class or interface that you are trying to use in your code.

Eclipse error: "The import XXX cannot be resolved"

If you get the error "The import XXX cannot be resolved" in Eclipse, it means that the class or package that you are trying to import cannot be found in the classpath of your project.

Eclipse java debugging: source not found

If you are trying to debug a Java application in Eclipse and you see the error "Source not found", it means that the source code for the class you are trying to debug is not available in the current project or the project build path.

Eclipse reported "Failed to load JNI shared library"

If you see the error "Failed to load JNI shared library" when starting Eclipse, it means that the Java Native Interface (JNI) library required by Eclipse cannot be found or loaded.

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).

Eclipse/Java code completion not working

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

Encoding as Base64 in Java

To encode a string as Base64 in Java, you can use the java.util.Base64 class. Here's an example of how you can use the Base64 class to encode a string:

Error - trustAnchors parameter must be non-empty

The trustAnchors parameter must be non-empty error typically occurs when you are trying to create an instance of the SSLContext class in Java and you pass an empty trust store as the trustAnchors parameter.

Error java.lang.OutOfMemoryError: GC overhead limit exceeded

The java.lang.OutOfMemoryError: GC overhead limit exceeded error occurs when the garbage collector is unable to free up enough memory to meet the memory allocation request of the application.

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.

Error:java: javacTask: source release 8 requires target release 1.8

This error message usually indicates that you are trying to compile your Java code with a version of the javac compiler that is not compatible with the version of the Java language that your code is written in.

Examples of GoF Design Patterns in Java's core libraries

There are many examples of the GoF (Gang of Four) design patterns in the core libraries of Java. Here are some examples:

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

Execution failed for task :app:compileDebugJavaWithJavac is an error message that can occur when you are trying to build an Android project in Android Studio.

Explanation of 'String args[]' and static in 'public static void main(String[] args)'

In the main() method in Java, String args[] is an array of strings that holds the command-line arguments passed to the program.

Extending from two classes

In Java, a class can only extend from one superclass (i.e., it can only have one direct parent class).

Extract source code from .jar file

To extract the source code from a .jar file, you can use a decompiler such as JD-GUI or Cavaj Java Decompiler.

Failed to install android-sdk: "java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema"

The java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema error can occur if the required Java XML Binding (JAXB) classes are not present on the classpath.

Failed to load the JNI shared Library (JDK)

This error can occur when you are trying to run a Java program and the Java Virtual Machine (JVM) cannot find the required shared libraries. There are a few different causes of this error and a few different things you can try to fix it:

File to byte[] in Java

To convert a file to a byte[] in Java, you can use the readAllBytes method of the Files class from the java.nio.file package. This method reads all the bytes from a file and returns them in a byte[]. Here's an example of how you can use this method:

Find first element by predicate

To find the first element in a list that matches a certain condition, you can use the stream() method to create a stream from the list, and then use the filter() method to specify the condition that the element should satisfy. Finally, you can use the fin

Finding the max/min value in an array of primitives using Java

To find the maximum value in an array of primitives in Java, you can use the Arrays.stream() method to create a stream from the array, and then use the Stream.max() method to find the maximum element in the stream. Here is an example of how to do this for