How-to articles, tricks, and solutions about JAVA

How do I get the file name from a String containing the Absolute file path?

To get the file name from a String containing the absolute file path in Java, you can use the following methods:

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 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:

How do I initialize a byte array in Java?

To initialize a byte array in Java, you can use the array initializer syntax, like this:

How do I invoke a Java method when given the method name as a string?

To invoke a Java method when given the method name as a string, you can use reflection. Reflection is a feature of the Java language that allows you to inspect and manipulate classes, fields, and methods at runtime.

How do I join two lists in Java?

There are several ways to join two lists in Java. Here are a few options:

How do I load a file from resource folder?

To load a file from the resource folder in a Java application, you can use the ClassLoader and getResourceAsStream method.

How do I make a delay in Java?

To make a delay in Java, you can use the Thread.sleep method which will pause the current thread for a specified number of milliseconds. Here's an example:

How do I make the method return type generic?

To make the return type of a method generic in Java, you can use a type parameter. A type parameter is a placeholder for a specific type that will be specified when the method is called.

How do I measure time elapsed in Java?

To measure time elapsed in Java, you can use the System.currentTimeMillis() method to get the current time in milliseconds, then subtract the start time from the end time to get the elapsed time.

How do I parse command line arguments in Java?

To parse command line arguments in Java, you can use the main() method of your application's entry point class.

How do I print my Java object without getting "SomeType@2f92e0f4"?

To print a Java object in a more readable format, you can override the toString() method in your class.

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.

How do I read / convert an InputStream into a String in Java?

There are several ways to read an InputStream and convert it to a String in Java. One way is to use the BufferedReader and InputStreamReader classes to read the InputStream line by line, and use a StringBuilder to construct the final String:

How do I remove repeated elements from ArrayList?

To remove repeated elements from an ArrayList in Java, you can use the removeAll method and pass it a Collection containing the elements to be removed.

How do I replace a character in a string in Java?

To replace a character in a string in Java, you can use the String.replace(char oldChar, char newChar) method.

How do I resolve ClassNotFoundException?

A ClassNotFoundException occurs when the Java virtual machine (JVM) is unable to find a class that has been referenced in your code. This can happen for a number of reasons, including:

How do I resolve the "java.net.BindException: Address already in use: JVM_Bind" error?

The java.net.BindException: Address already in use: JVM_Bind error occurs when you try to bind a socket to a local address and port, but the address and port are already in use by another process. This can happen when you try to start a server on a port

How do I reverse an int array in Java?

To reverse an int array in Java, you can use a loop to swap the elements at the beginning and end of the array, and then move the pointers inward until they meet in the middle of the array.

How do I save a String to a text file using Java?

To save a String to a text file in Java, you can use the write method of the FileWriter class. Here's an example of how you can do this:

How do I set environment variables from Java?

To set an environment variable from Java, you can use the System.setProperty() method.

How do I set the proxy to be used by the JVM

To set the proxy to be used by the Java Virtual Machine (JVM), you can use the -Dhttp.proxyHost, -Dhttp.proxyPort, and -Dhttp.nonProxyHosts options when starting the JVM.

How do I tell Gradle to use specific JDK version?

To specify the JDK version that Gradle should use, you can include the org.gradle.java.home property in the gradle.properties file in the root project directory.

How do I tell Maven to use the latest version of a dependency?

To tell Maven to use the latest version of a dependency, you can use the [RELEASE] or [LATEST] version range in the <version> element of the dependency in your pom.xml file.

How do I test a class that has private methods, fields or inner classes?

To test a class that has private methods, fields, or inner classes, you can do the following: