mkyong

mkyong

Software Engineer • Writer • Dad

20+ years hands-on, writing about Java, Spring, and AI. Every code example here is tested.

1,960 posts

Posts by mkyong

In Java, we can use Apache commons-text to escape the special characters in HTML entities. Special characters as follow: < > ” & pom.xml <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency> EscapeSpecialChar.java package com.mkyong.html; import org.apache.commons.text.StringEscapeUtils; // @deprecated as of 3.6, use commons-text StringEscapeUtils instead //import org.apache.commons.lang3.StringEscapeUtils; public class EscapeSpecialChar { public static void main(String[] args) { […]

Read more How to escape special characters in java?

In Java, we can use str.getBytes(StandardCharsets.UTF_8) to convert a String into a byte[]. String str = "This is a String"; // default charset, a bit dangerous byte[] output1 = str.getBytes(); // in old days, before java 1.7 byte[] output2 = str.getBytes(Charset.forName("UTF-8")); // the best , java 1.7+ , new class StandardCharsets byte[] output3 = str.getBytes(StandardCharsets.UTF_8); […]

Read more How to convert String to byte[] in Java?

In Java, we can use new String(bytes, StandardCharsets.UTF_8) to convert a byte[] to a String. // string to byte[] byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8); // byte[] to string String s = new String(bytes, StandardCharsets.UTF_8); Table of contents 1. byte[] in text and binary data 2. Convert byte[] to String (text data) 3. Convert byte[] to String […]

Read more How to convert byte[] array to String in Java

In this article, we show you how to use Java Cryptography Extension (JCE) to encrypt or decrypt a text via Data Encryption Standard (DES) mechanism. 1. DES Key Create a DES Key. KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey(); 2. Cipher Info Create a Cipher instance from Cipher class, specify the following information and […]

Read more JCE Encryption – Data Encryption Standard (DES) Tutorial

Often times, you may to use “mvn eclipse:eclipse -Dwtpversion=1.5” command to create a web project for Eclipse IDE, but you may encounter the following error messages. Unsupported WTP version: 1.5. This plugin currently supports only the following versions: 1.0 R7 D:\mkyong>mvn eclipse:eclipse -Dwtpversion=1.5 [INFO] Scanning for projects… [INFO] Searching repository for plugin with prefix: ‘eclipse’. […]

Read more Unsupported WTP version: 1.5. This plugin currently supports only the following versions: 1.0 R7

In some cases, you may need to generate Ant build file (build.xml) for your project integration purpose. For example, the klockwork code analysis tool is required Ant file to perform the static code analysis processes for java project. Maven Ant plugin Maven comes with a handy Ant plugin to generate Ant build file from Maven […]

Read more How to create an Ant build file from Maven (pom.xml)?

According to Apache Maven : Downloading in Maven is triggered by a project declaring a dependency that is not present in the local repository (or for a SNAPSHOT, when the remote repository contains one that is newer). By default, Maven will download from the central repository. In Maven, when you’re declared library does not exist […]

Read more How to download from Maven remote repository?

By default, Maven will get project dependencies from Maven Local Repository, if it is not found, Maven will get it from the Maven Central Repository Maven Central Repository URL – https://repo.maven.apache.org/maven2 Maven Central Repository Search – https://search.maven.org/ This is how the Maven central repository search website looks like : History This is how the Maven […]

Read more Where is Maven Central Repository?

Today when i debugging a Java Mail program, i find out one of the checking is about “Part.Attachment” and “Part.Inline” in email message. What is the different between Inline attachment and attachment? Inline Attachment Inline attachment usually is an attachment that we can see direclty within the email message body. For Example, In outlook express, […]

Read more Different between Inline and Attachment in Email

In Java, you can use following methods to get time in milliseconds Date class – getTime() method Calendar class – getTimeInMillis() method TimeMilisecond.java package com.mkyong.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TimeMilisecond { public static void main(String[] argv) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); String dateInString = "22-01-2015 […]

Read more How to get time in milliseconds in Java

In Java, you can use the following ways to measure elapsed time in Java. 1. System.nanoTime() This is the recommended solution to measure elapsed time in Java. ExecutionTime1.java package com.mkyong.time; import java.util.concurrent.TimeUnit; public class ExecutionTime1 { public static void main(String[] args) throws InterruptedException { //start long lStartTime = System.nanoTime(); //task calculation(); //end long lEndTime = […]

Read more How to calculate elapsed / execute time in Java

Regular expression pattern in Java always is the best method to validate an user’s phone number. Here i provide a regex pattern to determines if the phone number is in correct format, the pattern force starting with 3 digits follow by a “-” and 7 digits at the end. \\d{3}-\\d{7} Explanation \\d = only digit […]

Read more How to validate phone number in Java (regular expression)

Here i show you four ways to loop a List in Java. Iterator loop For loop For loop (Adcance) While loop package com.mkyong.core; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "Array 1", "Array 2", "Array 3" }; // convert array […]

Read more How to loop / iterate a List in Java

Java example to show you how to convert a Array to a List ArrayToList.java package com.mkyong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class ArrayToList { public static void main(String[] argv) { String sArray[] = new String[] { "A", "B", "C" }; // convert array to list #1 List<String> list = Arrays.asList(sArray); System.out.println(list); […]

Read more How to convert Array to List in Java

By default, Maven local repository is defaulted to ${user.home}/.m2/repository folder : Unix/Mac OS X – ~/.m2/repository Windows – C:\Users\{your-username}\.m2\repository When we compile a Maven project, Maven will download all the project’s dependency and plugin jars into the Maven local repository, save time for next compilation. 1. Find Maven Local Repository 1.1 If the default .m2 […]

Read more Where is Maven local repository?

A JDBC example to show you how to connect to a PostgreSQL database with a JDBC driver. Tested with: Java 8 PostgreSQL 11 PostgreSQL JDBC driver 42.2.5 1. Download PostgreSQL JDBC Driver Visit http://jdbc.postgresql.org/download.html to download the latest PostgreSQL JDBC Driver. 2. JDBC Connection 2.1 Make a connection to the PostgreSQL database. JDBCExample.java import java.sql.Connection; […]

Read more Connect to PostgreSQL with JDBC driver

When i received a new mail from yahoo mail box, my yahoo messenger will voice out a typical “de” voice and display a simple dialog box as “Yahoo! Mail 1 new message(s)” I just want my yahoo messenger to voice up a “You’ve got mail” voice whenever any new mail received. Here i show how […]

Read more How to set up “You’ve got mail” voice when receive yahoo mail.

MouseListener Mouse event processing is easy to implement, we need to implement MouseListener interface and declare all the interface methods. We can attached the MouseListener to a widget with addMouseListener() method. "widget control".addMouseListener(new MouseListener() { public void mouseDown(MouseEvent e) { System.out.println("Mouse Down."); } public void mouseUp(MouseEvent e) { System.out.println("Mouse Up."); } public void mouseDoubleClick(MouseEvent e) […]

Read more SWT – MouseListener & MouseAdapter Example

What is TabFolder? In SWT, TabFolder is a subclass of a Composite class. The TabFolder can include composite object into a single container through a tabbed index. How to create a TabFolder? Here i create a TabFolder composite, add a sashForm composite into a tab 1 and Group composite into a tab 2. import org.eclipse.swt.SWT; […]

Read more SWT – TabFolder Example

What is SashForm? In SWT, SashForm is similar with Group, however it added one more useful feature, it’s allow user to adjust the control size at runtime environment. The SashForm provide this feature by creating a movable line between child widget (button, label, text…). When user drag the “line” it will expand one widget size […]

Read more SWT – SashForm Example

What is Group In SWT, Group is a subclass of a Composite class. Group is used to improve application appearance and make whole application look more organized. It will draw a rectangular border around all it’s child widgets. Group widget support five styles (actually it is nothing much different) 1) SWT.SHADOW_IN 2) SWT.SHADOW_OUT 3) SWT.SHADOW_NONE […]

Read more SWT – Group Example

In SWT, button widget consists of five buttons style. 1) Normal Button – SWT.PUSH 2) Arrow Button – SWT.ARROW 3) Toggle Button – SWT.TOGGLE 4) Check Box button – SWT.CHECK 5) Radio Button – SWT.RADIO Here i demonstrate how to create those buttons in SWT import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class […]

Read more SWT – Button Example

What is Label? The Label is the most common and frequent use widget, it’s display static information such as String or Image, and no user input involve. How to create a Label widget? This code snippet will create a label at position x=100, y=50, width = 300, height = 30, and display “I am Label” […]

Read more SWT – Label Example

SWT is stand for Standard Widget Toolkit. I do not want to explain what the benefits of it , please search Google for it. Please access SWT Official Website if you want to know more about it. Here is the simple SWT Hello World program import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; public class […]

Read more SWT Hello World Example