How-to articles, tricks, and solutions about JAVA

Http Basic Authentication in Java using HttpClient?

To perform HTTP basic authentication in Java using the HttpClient library, you can use the UsernamePasswordCredentials class and the BasicCredentialsProvider class.

HTTP POST using JSON in Java

To make an HTTP POST request using JSON in Java, you can use the HttpURLConnection class available in the java.net package. Here's an example of how to do it:

HttpServletRequest get JSON POST data

To get JSON POST data from an HttpServletRequest object in Java, you can use the getReader method of the ServletRequest interface to read the request body as a BufferedReader and then use the readLine method to read the data as a string.

I get exception when using Thread.sleep(x) or wait()

If you are getting an exception when using Thread.sleep(x) or wait(), it could be because one of these methods has been interrupted.

I need to convert an int variable to double

To convert an int to a double, you can use the doubleValue() method of the Integer class.

If statement with String comparison fails

If an if statement with a string comparison fails in Java, it usually means that the strings being compared are not equal.

Ignoring new fields on JSON objects using Jackson

If you want to ignore new fields on JSON objects when using Jackson, you can use the @JsonIgnoreProperties annotation on your Java object.

Implements vs extends: When to use? What's the difference?

In Java, the extends keyword is used to inherit from a superclass, and the implements keyword is used to implement an interface. Here are the key differences between the two:

Import a custom class in Java

To use a custom class in your Java code, you will need to import it at the beginning of your source file.

Import Maven dependencies in IntelliJ IDEA

To import Maven dependencies in IntelliJ IDEA, you can follow these steps:

In java how to get substring from a string till a character c?

To get a substring from a string in Java up until a certain character, you can use the indexOf method to find the index of the character and then use the substring method to extract the substring.

In Java, how do I check if a string contains a substring (ignoring case)?

To check if a string contains a substring (ignoring case) in Java, you can use the contains method of the String class and the equalsIgnoreCase method.

Including all the jars in a directory within the Java classpath

To include all the jars in a directory within the Java classpath, you can use the -cp or -classpath command-line option and specify the directory containing the jars.

Including dependencies in a jar with Maven

To include dependencies in a JAR file with Maven, you can use the maven-assembly-plugin to create an assembly that includes all the dependencies of your project.

Initializing multiple variables to the same value in Java

In Java, you can initialize multiple variables to the same value in a single statement by separating the variables with a comma.

Installing Java 7 on Ubuntu

To install Java 7 on Ubuntu, you can follow these steps:

Integer division: How do you produce a double?

In Java, integer division always produces an integer result, even if the dividend is a floating-point number.

intellij incorrectly saying no beans of type found for autowired repository

If IntelliJ is saying "No beans of type 'X' found for autowiring" for a repository that you are trying to autowire, it means that the Spring application context does not contain a bean of the specified type.

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.

Is Java "pass-by-reference" or "pass-by-value"?

In Java, arguments are passed to methods by value. This means that when you pass an argument to a method, the method receives a copy of the argument rather than a reference to the original object.

Is there a destructor for Java?

Java does not have a destructor like some other programming languages.

Iterate through a HashMap

There are several ways to iterate through a HashMap in Java: Using the for-each loop

Jackson with JSON: Unrecognized field, not marked as ignorable

If you are using the Jackson library to parse JSON in Java and you get the error "Unrecognized field, not marked as ignorable", it means that you are trying to parse a JSON object that has a field that is not recognized by your Java object.