Skip to main content

Posts

Showing posts with the label java

Java, how to create a list with a single element

 I wanted to create a Java List with a single element. Yet, I wanted to add more elements later. So, I was looking for a couple of ways to do this. So far there are multiple elegant ways to create a list with a single element with a one-liner. Not so much for a modifiable list though. Here's what I gathered so far. Followings are a few ways of creating a list with strictly a single entry. Can't add more elements. 1. Collections.singletonList() This returns an immutable list that cannot be modified or add more elements. // An immutable list containing only the specified object. List<String> oneEntryList = Collections. singletonList ( "one" ) ; oneEntryList.add( "two" ) ; // throws UnsupportedOperationException 2.  Arrays.asList() This returns a fixed-size list consisting of the number of elements provided as arguments. The number of elements provided would be an array hence the size is fixed to the length of the array. // Returns a fixed-size list List...

A Silly New Year Wish with a Java code

TJ's silly new year wish Happy New Year y'all!!! package com.tjisblogging ;   import java.time.LocalDate ; import java.time.Month ; import java.util.Vector ;   public class NewYear {   private static final LocalDate END_OF_2022 = LocalDate . of ( 2022 , Month. DECEMBER , 31 ) ;   public static void main ( String [ ] args ) { Life yourLife = Life. getInstance ( ) ; while ( ! LocalDate. now ( ) . equals ( END_OF_2022 ) ) { yourLife. push ( "Love" ) ; yourLife. push ( "Joy" ) ; yourLife. push ( "Happiness" ) ; yourLife. push ( "Prosperity" ) ; yourLife. push ( "Money" ) ; yourLife. push ( "Health" ) ; } } }   class Life { private Vector < String > lifeQueue = new Vector <> ( ) ; private static Life instance = new Life ( ) ;   private Life ( ) { }  ...

How to resolve Log4j CVE-2021-44228 in Spring Boot

 As you may know, Log4j released a patch (actually a couple of patches) recently for a vulnerability  [CVE-2021-44228] that was identified in their library. Since a lot of Spring boot applications are out there using Log4j 2.x series. It is better to fix them as soon as possible.  Also, this article is considering you are using Maven for dependency management. Fixing this in the Spring boot applications is easy. It is just adding a version property for the pom.xml.  As of this date of writing, the latest updated Log4J version is  2.17.1 . So the fix is to update the version property with this. <properties>     <log4j2.version>2.17.1</log4j2.version>     <log4j.version>2.17.1</log4j.version> </properties> If you are using BOM, instead of spring boot parent dependency, update it as follows. <dependencyManagement>     <dependencies>         <dependency>   ...

How to serialize a class with static variables in Java

TL;DR You can't! Instead; Change the static variables into a none static member variable of a singleton class. By making the variables holding class to a singleton, still, it caters to the requirement of keeping your variables having a single reference for the entire application. (This is one of the ways of doing it, surely there are more other elegant ways and crude hacks of doing this depending on the situation) This is a problem that a scholar asked me for some help with. We have a simple use case where there's a configuration class that it requires to hold some values used in application-wide. So the easiest way (not the best) somebody could think would be making the variables static, so it would be only a single reference for it throughout the whole application. Let's imagine the configuration class would look like below. import java.io.Serializable ; public class BranchConfig implements Serializable { private static String branchNa...

Java String NullPointerException safe equals check

If you compare two Strings in Java, normally you would use the equals method to compare them to see if they are similar. This will be some common use case for input validations. However, if you don't do it properly it may cause you to throw a NullPointerException. This is pretty much like common sense, but I've seen even more experienced developers make this mistake. Let's consider the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class DemoApplication { final static String theStringIknow = "Hello" ; public static void myTestMethod ( String someString ) { //do not do this if ( someString . equals ( theStringIknow )) { System . out . println ( "Same same" ); } } public static void main ( String [] args ) { String testString = "Hello" ; myTestMethod ( testString ); } } In myTestMethod, I'm checking if the String in the a...

How to fix SSLHandshakeException PKIX path building failed in Java

TL ; DR 1. Extract the public certificate of the website/API that you are trying to connect from your Java application. Steps are mentioned in this post 2. Use the Java keytool to install the extracted certificate into the "cacerts" file (Trust store) keytool -import -trustcacerts -alias <domain name> -file <public certificate>.cert -keystore /path_to_java_home/jre/lib/security/cacerts -storepass changeit 3. Restart your Java application Exception A typical exception stack trace would look like below. javax.net.ssl. SSLHandshakeException : sun.security.validator.ValidatorException: PKIX path building failed : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshake...