JavaFX Animated Ball Example

The Bouncing Ball is the “Hello World” of animations in JavaFx. It’s simple to write, easy to understand and reveals the potential of JavaFx even from this primitive stage. We will start by creating a moving ball that will set the basis for the bouncing ball that will follow. 1. Moving Ball Example Apart from …

Read more

Spring MVC – How to handle max upload size exceeded exception

In Spring, you can declare a @ControllerAdvice to catch the ugly max upload size exceeded exception like this : Solution Depends the types of multipartResolver : StandardServletMultipartResolver – catch MultipartException, refer to this example. CommonsMultipartResolver – catch MaxUploadSizeExceededException – refer to this example. GlobalExceptionHandler.java package com.mkyong.exception; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.multipart.MultipartException; import …

Read more

Spring MVC file upload example – Commons FileUpload

This article will shows you how to use CommonsMultipartResolver to handle file upload in Spring MVC web application. Tools used : Spring 4.3.5.RELEASE commons-fileupload 1.3.2 Maven 3 Tomcat 7 or 8 and Jetty 8, 9 Note For StandardServletMultipartResolver – file upload using Servlet 3.0 multipart request parsing, please refer to this Spring MVC file upload …

Read more

Spring file upload and connection reset issue

A Spring servlet initializer to configure the file upload limit, 5mb per file and 10mb per request. MyWebInitializer.java public class MyWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { private int maxUploadSizeInMb = 5 * 1024 * 1024; // 5 MB //… @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { // upload temp file will put here File uploadDirectory = new File(System.getProperty("java.io.tmpdir")); …

Read more

Spring Boot – Deploy WAR file to Tomcat

In this article, we will show you how to create a Spring Boot traditional WAR file and deploy to a Tomcat servlet container. In Spring Boot, to create a WAR for deployment, we need 3 steps: Extends SpringBootServletInitializer Marked the embedded servlet container as provided. Update packaging to war Tested with Spring Boot 2.1.2.RELEASE Tomcat …

Read more

Java – Digital Signatures example

In Asymmetric Cryptography example we discussed the use of Public Key Pair in Cryptography. Another important use of the Public Key Infrastructure is in Digital Signatures. Digital Signatures are the digital equivalent of handwritten signatures with one important difference; they are not unique but come as a product of the message. A valid digital signature …

Read more

How to copy an Array in Java

The methods described below are only applicable to one dimensional arrays. Before we talk about the different ways to copy an array in Java we will show you how NOT to copy an Array. How NOT to copy an Array in Java Arrays in Java are Objects. If you try to treat them as variables… …

Read more

Java Swing – JOptionPane showOptionDialog example

The showOptionDialog method of JOptionPane is the Grand Unification of showConfirmDialog, showInputDialog and showMessageDialog. The showOptionDialog returns an integer which represents the position of the user’s choice in the Object[]. Note If you want to read more about the different showXxxDialog methods, refer to Java Swing – JOptionPane showConfirmDialog example Java Swing – JOptionPane showInputDialog …

Read more

Java 8 – Math Exact examples

Java 8 introduced new methods in the Math class that will throw an ArithmeticException to handle overflows. These methods consist of addExact, substractExact, multiplyExact, incrementExact, decrementExact and negateExact with int and long arguments. In addition, there’s a static toIntExact method to convert a long value to an int that also throws ArithmeticException. Before Java 8 …

Read more

Spring Boot @ConfigurationProperties example

Spring Boot @ConfigurationProperties lets developers map or bind the entire external configuration values in .properties or .yml files to Java objects. Table of contents: 1. Access a single value using @Value 2. @ConfigurationProperties 3. Add JSR303 Validation 3.1 Add JSR 303 dependencies 3.2 @ConfigurationProperties and @Validated 4. Testing @ConfigurationProperties 5. Download Source Code 6. References …

Read more

Spring Boot Logging Example

This article will demonstrate Spring Boot Logging using the Logback SLF4J implementation. Technologies used: Spring Boot 3.2.2 Java 17 Maven 3 Table of contents: 1. Project Directory 2. Spring Boot Logging Dependencies 3. Spring Boot Logging using Logback 3.1. Log Example 3.2 Log variables 4. Log to File 4.1 File Rotation 5. Log Levels 6. …

Read more

wget on Mac OS X

By default, there is no wget on Mac OS X. $ wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz -bash: wget: command not found On Mac OS X, the equivalent of Linux’s wget is curl -O $ curl -O http://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 49 9112k 49 …

Read more

iText – Read and Write PDF in Java

This article talks about reading and writing PDF using iText PDF library. pom.xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> P.S Tested with iTextPdf 5.5.10 1. iText – Write PDF iText PdfWriter example to write content to a PDF file. PdfWriteExample.java package com.mkyong; import com.itextpdf.text.*; import com.itextpdf.text.pdf.PdfWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class …

Read more

Intellij IDEA – Spring boot reload static file is not working

In Eclipse, just include the Spring Boot Dev Tools dependency, then the hot swapping and static file reload will be enabled magically. For Intellij IDE, we need extra steps to enable it. 1. Spring Boot Dev Tools With Spring Boot Dev Tools enabled : Any changes to views or resources can be seen in the …

Read more

Spring Boot – Which main class to start

If Spring Boot project contains multiple main classes, Spring Boot will fail to start or packag for deployment. Terminal $ mvn package #or $ mvn spring-boot:run Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run (default-cli) Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.4.2.RELEASE:run failed: Unable to find a single main class from the following candidates [com.mkyong.Test, com.mkyong.SpringBootWebApplication] -> [Help 1] Maven …

Read more

Spring Boot – Jetty as embedded server

By default, Spring Boot use Tomcat as the default embedded server, to change it to Jetty, just exclude Tomcat and include Jetty like this : 1. spring-boot-starter-web pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> 2. spring-boot-starter-thymeleaf pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> …

Read more

How to change the default port in Spring Boot

Default, Spring Boot web application starts embedded web server (Tomcat, Jetty, etc.) in port 8080. And we can configure or change the port using the server.port property. Table of contents 1. application.properties 2. application.yml 3. Command Line 4. Environment Variable 5. WebServerFactoryCustomizer 6. Download Source Code 7. References P.S Tested with Spring Boot 1, 2, …

Read more

Spring Boot – How to change Context Path

In Spring Boot, to change the context path, update server.contextPath properties. The following examples update the context path from / to /mkyong or http://localhost:8080/mkyong Note By default, the context path is “/”. P.S Tested with Spring Boot 1.4.2.RELEASE 1. Properties & Yaml 1.1 Update via a properties file. /src/main/resources/application.properties server.port=8080 server.contextPath=/mkyong 1.2 Update via a …

Read more

Spring Boot Hello World Example – Thymeleaf

In this article, we will show you how to develop a Spring Boot web application, using Thymeleaf view, embedded Tomcat and package it as an executable JAR file. Technologies used : Spring Boot 2.1.2.RELEASE Spring 5.1.4.RELEASE Thymeleaf 3.0.11.RELEASE Tomcat embed 9.0.14 JUnit 4.12 Maven 3 Java 8 1. Project Directory 2. Maven Put spring-boot-starter-web and …

Read more

JPA optimistic lock exception in Java Development

This post explains the JPA technology and its use in java development. Experts of java development India are explaining the use case of technologies- JPA and Hibernate, MySql database, Maven. Read this post and know what they want to say. Technology: JPA stands for the Java Persistence API which is the standard from Sun Micro …

Read more

Java – Hybrid Cryptography example

Hybrid Cryptography is the silver lining between safe, but slow cryptography over big data (Asymmetric Cryptography) and unsafe but fast cryptography (Symmetric Cryptography). Hybrid Cryptography combines the speed of One-Key encryption and decryption along with the security that the Public-Private Key pair provides and thus considered a highly secure type of encryption. The most common …

Read more

Java – Asymmetric Cryptography example

Asymmetric Cryptography, also known as Public Key Cryptography, is an encryption system in which two different but uniquely related cryptographic keys are used. The data encrypted using one key can be decrypted with the other. These keys are known as Public and Private Key Pair, and as the name implies the private key must remain …

Read more

Spring Boot Hello World Example – JSP

A Spring Boot web application example, using embedded Tomcat + JSP template, and package as an executable WAR file. Technologies used : Spring Boot 1.4.2.RELEASE Spring 4.3.4.RELEASE Tomcat Embed 8.5.6 Maven 3 Java 8 1. Project Directory Create the following folders manually : 2. Project Dependencies Maven example. Read comments for self-explanatory. pom.xml <?xml version="1.0" …

Read more

Java Swing – JOptionPane showConfirmDialog example

This is a review of the showConfirmDialog() method of JOptionPane Class. This method is a quick and easy way to get user input by asking a confirming question, like yes/no/cancel . The showConfirmDialog() can be called using the following combinations of parameters: Component, Object Component, Object, String, int Component, Object, String, int, int Component, Object, …

Read more

Java Swing – JOptionPane showMessageDialog example

This is a review of the showMessageDialog() method of JOptionPane Class. This method is a quick and easy way to tell the user about something that has happened . The showMessageDialog() can be called using the following combinations of parameters: Component, Object Component, Object, String, int Component, Object, String, int, Icon Component – The first …

Read more

Spring Boot web JSP – No Java compiler available

Spring boot web application with embeded Tomcat and JSP. Run and access the JSP page, but hits the following errors $ mvn spring-boot:run … java.lang.IllegalStateException: No Java compiler available at org.apache.jasper.JspCompilationContext.createCompiler(JspCompilationContext.java:235) ~[tomcat-embed-jasper-8.5.6.jar:8.5.6] at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592) ~[tomcat-embed-jasper-8.5.6.jar:8.5.6] at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368) ~[tomcat-embed-jasper-8.5.6.jar:8.5.6] at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) ~[tomcat-embed-jasper-8.5.6.jar:8.5.6] at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) ~[tomcat-embed-jasper-8.5.6.jar:8.5.6] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.5.6.jar:8.5.6] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) [tomcat-embed-core-8.5.6.jar:8.5.6] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) [tomcat-embed-core-8.5.6.jar:8.5.6] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) …

Read more

Java – Symmetric-Key Cryptography example

Symmetric-Key Cryptography is an encryption system in which the same key is used for the encoding and decoding of the data. The safe distribution of the key is one of the drawbacks of this method, but what it lacks in security it gains in time complexity. One should always assume that the encryption algorithms are …

Read more

Java – How to create strong random numbers

SecureRandom class in Java provides a cryptographically secure pseudo – random number generator and its intended use is for security sensitive applications. In this example, we will not use it for its intended purpose, but rather present its methods in a simple password generator. 1.Password Generator Using Secure Random A convention, we made for our …

Read more

Java – Check if Array contains a certain value?

Java examples to check if an Array (String or Primitive type) contains a certain values, updated with Java 8 stream APIs. 1. String Arrays 1.1 Check if a String Array contains a certain value “A”. StringArrayExample1.java package com.mkyong.core; import java.util.Arrays; import java.util.List; public class StringArrayExample1 { public static void main(String[] args) { String[] alphabet = …

Read more

CSF – How to limit the number of connections per IP address

In the ConfigServer Security & Firewall (CSF) configuration file, update the CT_LIMIT value to limit the number of connections per IP address. This is a simple trick to prevent some types of Denial of Service (DOS) attack. Note To stop the Denial of Service (DoS) attack immediately, read this null route example. 1. /etc/csf/csf.conf SSH …

Read more

Java Date Time Tutorials

A collection of Java date and time examples. 1. Java Date Time APIs In old days, we use the following classic Date and Calendar APIs to represent and manipulate date. java.util.Date – date and time, print with default time-zone. java.util.Calendar – date and time, more methods to manipulate date. java.text.SimpleDateFormat – formatting (date -> text), …

Read more

Java 8 – TemporalAdjusters examples

In Java 8, you can use the predefined java.time.temporal.TemporalAdjusters to adjust a date or Temporal 1. TemporalAdjusters Example to move a date to firstDayOfMonth, firstDayOfNextMonth, next Monday and etc. TestDate.java package com.mkyong.time; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; public class TestDate { public static void main(String[] args) { LocalDate localDate = LocalDate.now(); System.out.println("current date : …

Read more