Skip to main content

Posts

Git menu is not shown up in IntelliJ IDEA 2023.1.1 (Community Edition)

  Problem :  If you have upgraded your IntelliJ IDEA to IntelliJ IDEA 2023.1.1 (Community Edition), then most probably the Git menu will not be shown up. Solution : a) Your project is running in safe mode, you need to trust this project b) Try the new UI of IntelliJ like the following

jdk 21: The new features in Java 21

JDK 21 is scheduled to be released in September 2023 as specified in    JSR 395 . The main 3 features included in JDK 21  are  a) Sequenced Collections According to  OpenJDK , a sequenced collection has first and last elements, and the elements between them have successors and predecessors. A sequenced collection supports common operations at either end, and it supports processing the elements from first to last and from last to first (i.e., forward and reverse). b)  Virtual Threads According to OpenJDK ,  Virtual threads are lightweight threads that dramatically reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. c)  String Templates (Preview)  According to OpenJDK ,  Enhance the Java programming language with  string templates . String templates complement Java's existing string literals and text blocks by coupling literal text with embedded expressions and  template process...

Java: How to split a string based on . (DOT) , (COMMA) : (COLON)

In Java language, DOT is a special character in the regular expression. If you have the following string String str = "He is a boy. He is my friend"; The following statement will return an empty string array String tokens[] = s.split("."); Solution: You can split a string based on DOT in the following two ways a) String tokens[] = str.split("[.]"); b) String tokens[] = str.split("\\."); The same solution also applies to the following characters a) WHITE SPACE  b) COMMA c) COLON Complete Example: public class StringSplit {      public static void main(String[] args ) {           String str = "He is a boy.He is my friend" ;           String tokens [] = str .split( "[.]" );           System. out .println( tokens [0]);           System. out .println( tokens [1]);          //or using escape character ...

git : unable to unlink old invalid argument

  Problem : If you get the following error while git checkout or git pull  git unable to unlink old [file name] invalid argument Solution Most probably the file is opened and locked in another application. Close that application and then try again, you will successfully continue with the respective git command.

Factory method 'filterChain' threw exception with message: This object has already been built

  Problem org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration': Unsatisfied dependency expressed through method 'setFilterChains' parameter 0: Error creating bean with name 'filterChain' defined in class path resource [com/.class]: Failed to instantiate [org.springframework.security.web.SecurityFilterChain]: Factory method 'filterChain' threw exception with message: This object has already been built at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments( AutowiredAnnotationBeanPostProcessor.java:817 ) ~[spring-beans-6.0.4.jar:6.0.4] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject( AutowiredAnnotationBeanPostProcessor.java:769 ) ~[spring-beans-6.0.4.jar:6.0.4] at org.spri...

NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String

  Exception NoSuchMethodError: javax.xml.ws.WebFault.messageName()Ljava/lang/String Solution You are most probably using jdk6 or earlier versions of Java if you encounter this exception. You can fix this exception using the following steps a) Download the latest jax-ws b) Place the downloaded jar inside jre/lib/ endorsed folder OR c) Please the download jar file inside the web server lib endorsed folder like if using JBoss then place it inside jboss\lib\ endorsed if using Tomcat then place it inside tomcat\lib\ endorsed

JBoss : Transaction is not active: tx=TransactionImple < ac, BasicAction: abc status: ActionStatus.ABORTED >

 Problem  Closing a connection for you.  Please close them yourself: org.jboss.resource.adapter.jdbc.jdk5.WrappedConnectionJDK5@6e3a0a96 2021-11-16 12:23:43,715 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] (Thread-1371) [com.arjuna.ats.arjuna.coordinator.TransactionReaper_7] - TransactionReaper::doCancellations worker Thread[Thread-1371,5,jboss] successfully canceled TX a11267b:ff8e:61915e7e:1413e57 2021-11-16 12:23:43,715 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] (Thread-1371) [com.arjuna.ats.arjuna.coordinator.BasicAction_58] - Abort of action id a11267b:ff8e:61915e7e:1413e5d invoked while multiple threads active within it. 2021-11-16 12:23:43,715 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI18N] (Thread-1371) [com.arjuna.ats.arjuna.coordinator.CheckedAction_2] - CheckedAction::check - atomic action a11267b:ff8e:61915e7e:1413e5d aborting with 1 threads active! 2021-11-16 12:23:43,731 WARN  [com.arjuna.ats.arjuna.logging.arjLoggerI1...

Intellij : How to add @author comment to every new class

 Introduction In this tutorial, we will learn how to add @author comments to every new class that we create. We can achieve it using either of the following two solutions Solution 1:  Automatically add @author comments to every new class using Files and Code Templates Open File -> Settings -> Editor -> File and Code Templates -> Includes Click on Includes . Under File Header , enter the following comments text /**  * @author ${USER}  * @Date ${DATE}   */ Intellij - add @author comments Solution 2: Autocompletion of @author Open File  ->  Settings  ->  Editor  -> Live Templates Select Java and then click on + button In Abbreviation, enter @a In template text , enter the following comments           /**             * @author ${USER}             * @Date ${DATE}            */ In o...

Java : How to convert String to Date?

 Introduction In this post, we will learn how to convert Java String to Date  Example: import java.util.Date; public class DateConversion { public static void main(String[] args) {      SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");      try { Date date = sdf.parse("19-09-2020"); System.out.println(date);      } catch (Exception ex) { ex.printStackTrace();      } } } In Java 8 and onward versions, we can use the LocalDate and DateTimeFormatter class to convert a String to a LocalDate object. We will use the parse method of the LocalDate class to perform this conversion Example (using Java 8+ versions) import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateConversion { public static void main(String[] args) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); try { LocalDate date = LocalDate.parse("19-09-2020",formatt...

Java : How to convert java.util.Date to java.sql.Date?

 Introduction In this article, we will learn how to convert java.util.Date to java.sql.Date Example: import java.util.Calendar; import java.util.Date; public class DateConversion { public static void main(String[] args) { Date date = Calendar.getInstance().getTime(); java.sql.Date sqlDate = new java.sql.Date( date.getTime()); } } Please note that both these Date classes are outdated now. Use java.Time  classes instead of legacy java.util.Date & java.sql.Date with JDBC 4.2 or later. Use Instant class instead of java.util.Date Use LocalDate instead of java.sql.Date

Java : How to convert java.util.Date to Gregorian Calendar Date Format?

Introduction Below example demonstrates how to convert java.util.Date to Gregorian Calendar  Example import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class DateToGregorianCalendar { public static void main(String[] args) { Date date = Calendar.getInstance().getTime(); GregorianCalendar gregorianCalendar = new GregorianCalendar(); gregorianCalendar.setTime(date); } }

ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view

 Problem ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view 12054. 00000 -  "cannot set the ON COMMIT refresh attribute for the materialized view" *Cause:    The materialized view did not satisfy conditions for refresh at commit time. *Action:   Specify only valid options. Solution  You can not use the DISTINCT keyword in your Materialized View query, rather use Group By instead of distinct You can not use the standard JOIN in your query, instead, you should use the old-styled Join like A, B where A.ID = B.ID     

Spring Boot Maven plugin - How to deploy WAR to an external tomcat's webapp folder?

 Introduction Spring Boot Maven plugin by default generates the WAR file inside the target folder. In this post, I will explain how to copy the generated war file to an external Tomcat's webapps folder using Spring Boot Maven plugin. In my earliest post , you can learn how to package a Spring Boot application as WAR.  Solution Step 1  Follow this link to specify the packaging as WAR in pom.xml and also to configure the  SpringBootServletInitializer class Step 2 (optional) Specify a cleaner name for WAR using the finalName tag in pom.xml  <finalName>auth</finalName> Step 3 Specify the outputDirectory directory      <build> <finalname>auth</finalname> <plugins> <plugin>           <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration>           ...

Error: Could not find or load main class in IntelliJ IDE 2020.1.3

 Error Could not find or load main class in IntelliJ IDE 2020.1.3 Solution Step 1 Right-click on the source folder  src (Normal Java projects) java ( Java Maven project) scala (Scala Maven projects) Step 2 Select Mark Directory As Step 3 Select Sources Root IntelliJ Mark Directory as Sources Root Hope this post helps you to solve your problem.

How to get the length of a Collection in the JSF expression language?

 Problem How to get the length of a Collection in the JSF expression language? Solution There are two ways to get the length of a Collection i.e. List, Set, etc in the JSF expression language a) Define a method in the Bean In your bean declare a method to return the length of a collection @Named("MyBean ") @SessionScoped public class MyBean {     private List list;     .     .     .     public int getCollectionLength() {       return  list.size();     } } b) Using Facelets,the length function #{ fn:length(MyBean.list) }

How to convert Enum to List of String in Java 8?

Problem  How to convert Enum to List of String in Java 8?  Solution  Using the Java 8 Streams, its map and collect functions we can convert Enum to List of Strings  Code   import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class EnumToStringList { public enum Alpha { A,B,C,D; }  public static void main(String[] args) { List<String> strings = Stream.of(Alpha.values()).map(                                               Alpha::name).collect( Collectors.toList()); } }

Java 13 - How to Concatenate Text Blocks

Introduction If you have not tried the Text Blocks feature introduced in Java 13 then the following post will help you to get started Java 13 - Example to use Text Blocks for Multi line String literals We can concatenate TextBlocks as we do with normal String as demonstrated in the following example. Example public class TextBlock { public static void main(String[] args) { String html = """             <html>                 <body>                  """ +                 """   <h1> Header One </h1>                 <p> Paragraph One </p>               """ +                 """                 <h1> Header...

Java 13 - Example to use Text Blocks for Multi line String literals

Introduction Lengthy Strings in Java code becomes hard to read. Multiline String literals can be XML, JSON, HTML, SQL queries, Hibernate or JPA queries, etc. Example String html = " <html>\r\n" + " <body>\r\n" + " <p>Hello, world</p>\r\n" + " </body>\r\n" + " </html>\r\n" ; Solution Thanks to the Text blocks feature introduced in Java 13. Now, these multiline String literals are more presentable in Java code. 1. Configuring Eclipse for Text Blocks 1.1 Requirments JDK 13 Eclipse Version: 2020-03 (4.15.0) 1.2 Eclipse Error 1 String literal is not properly closed by a double-quote 1.2.1 Solution Change the source of your program to 13 as shown in the following screenshot Java Source 13 1.3 Eclipse Error 2 Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable 1.3.2 Eclipse Erro...