Example to run multiple jobs in Quartz

In this example, we show you how to declare multiple Quartz jobs via Quartz APIs, Quartz XML and Spring. In Quartz scheduler framework, each job will be attached to an unique trigger, and run it by scheduler. P.S In Quartz, one trigger for multiple jobs is not possible. (Correct me if this is wrong.) 1. …

Read more

How to work with Java 6’s NavigableSet and NavigableMap

You can use latest Java 6’s Collection API to navigate a set and Map collections. These API gives a lot of flexibility to find out required result from the collection. 1. NavigableMap Example package com.example.collection; import java.util.NavigableMap; import java.util.TreeMap; public class NavigableMapDemo { public static void main(String[] args) { NavigableMap<String,Integer> navigableMap=new TreeMap<String, Integer>(); navigableMap.put("X", 500); …

Read more

How to use reflection to copy properties from Pojo to other Java Beans

Sometimes we need copy properties from a Java class to other, we can do it this manually or with our own reflection implementation, but in this case use us reflection for automate it with a utility from apache Requirements commons-beanutils , you can download from here http://commons.apache.org/beanutils/ commons-loging , you can download from here http://commons.apache.org/logging/ …

Read more

How to run a task periodically in Java

Some java application need to execute a method between a regular interval of time. For example GUI application should update some information from database. 1. Scheduler Task For this functionality, You should create a class extending TimerTask(available in java.util package). TimerTask is a abstract class. Write your code in public void run() method that you …

Read more

Quartz 2 JobListener example

In this tutorial, we will show you how to create a JobListener, to keep track the running jobs status, like when the job is finished. P.S This example is tested with Quartz 2.1.5 1. Quartz Job Job, print a simple message, and throw a JobExecutionException for testing. File : HelloJob.java package com.mkyong.quartz; import org.quartz.Job; import …

Read more

Quartz 2 scheduler tutorial

Quartz, enterprise scheduler job framework, to help Java application to scheduler a job/task to run at a specified date and time. This tutorial show you how to develop a scheduler job using latest Quartz library 2.1.5. Note Quartz 2 involves significant API changed, read this for older Quartz 1.6.3 example. 1. Download Quartz You can …

Read more

Java and zip code example

Recently, answered an question about leading zero problem ZipCode, what is the most proper data type for country’s zip code? 1. ZipCode – int In Java, some claimed it should declare as “int”, and use DecimalFormat to format and display the leading zero. For example, package com.mkyong; import java.text.DecimalFormat; public class ZipCodeExample { public static …

Read more

Quartz : org.quartz.SchedulerConfigException: Thread count must be > 0

Working with Quartz 2, when running the project, hits following error message? org.quartz.SchedulerConfigException: Thread count must be > 0 at org.quartz.simpl.SimpleThreadPool.initialize(SimpleThreadPool.java:245) at org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1255) at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1484) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:680) Solution You have defined a “quartz.properties” file, and override the …

Read more

How to get URL content in Java

In this Java example, we show you how to get content of a page from URL “mkyong.com” and save it into local file drive, named “test.html”. package com.mkyong; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class GetURLContent { public static void main(String[] args) …

Read more

Java try-with-resources example

This article shows you how to use try-with-resources in Java. Table of contents: 1 Java try-with-resources before and after 2. try-with-resources with single resource 3. try-with-resources with multiple resources 3.1 JDBC example. 4. Custom resource with AutoCloseable interface 5. Resource open and closing order 6. Java 9 – final or effectively final variables Download Source …

Read more

How to read file in Java – FileInputStream

In Java, we use FileInputStream to read bytes from a file, such as an image file or binary file. Topics FileInputStream – Read a file FileInputStream – Remaining bytes FileInputStream – Better performance FileInputStream vs BufferedInputStream InputStreamReader – Convert FileInputStream to Reader FileInputStream – Read a Unicode file Note However, all the below examples use …

Read more

How to loop ArrayList in Java

No nonsense, four ways to loop ArrayList in Java For loop For loop (Advance) While loop Iterator loop package com.mkyong.core; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class ArrayListLoopingExample { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("Text 1"); list.add("Text 2"); list.add("Text 3"); System.out.println("#1 normal for loop"); for (int i = …

Read more

Convert JSON string to Map using Jackson

This article uses the Jackson framework to parse JSON strings to a Map in Java. Table of contents: 1. Download Jackson 2. Convert JSON string to Map 3. Convert Map to JSON string 4. Download Source Code 5. References P.S Tested with Jackson 2.17.0 1. Download Jackson Simply declare jackson-databind in the pom.xml, and it …

Read more

Read and write JSON using JSON.simple

This article shows how to read and write JSON using JSON.simple. Table of contents: 1. Setup JSON.simple 2. Write JSON to File using JSON.simple 3. Read JSON from File using JSON.simple 4. Java object to JSON using JSON.simple 5. JSON to Java Object using JSON.simple 6. Download Source Code 7. References P.S Tested with json-simple …

Read more

Gson Streaming APIs to read and write JSON

Streaming APIs are efficient ways of processing large JSON files or data without loading the entire document into memory. Gson provides JsonReader and JsonWriter classes for JSON streaming. Gson’s Streaming API JsonWriter – Write JSON as a stream. JsonReader – Read JSON as a stream. Table of contents: 1. Setup Google Gson 1. Write JSON …

Read more

Jackson Streaming API examples

This article shows how to use Jackson’s Streaming API for both reading from and writing to JSON. Jackson’s Streaming API JsonGenerator – Write JSON data JsonParser – Read and Parse JSON data Table of contents: 1. Download Jackson 2. Write JSON using JsonGenerator 2. Write JSON Array using JsonGenerator 3. Read JSON using JsonParser 4. …

Read more

How to enable pretty print JSON with Jackson

In Jackson, we can use writerWithDefaultPrettyPrinter() to pretty print the JSON strings. Table of contents: 1. Setup Jackson 2. Compact print JSON (Default) 3. Pretty print JSON with Jackson 4. Pretty print JSON with Jackson (Globally) 5. Download Source Code 6. References P.S Tested with Jackson 2.17.0 1. Setup Jackson Puts jackson-databind at pom.xml. pom.xml …

Read more

How to pretty print JSON using Gson

This article shows how to pretty print JSON using Gson. Table of contents: 1. Setup Google Gson 2. Default Compact Print JSON 3. Pretty Print JSON using Gson 4. Download Source Code 5. References P.S Tested with Gson 2.10.1 1. Setup Google Gson Declare gson in the pom.xml. pom.xml <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> </dependency> 2. …

Read more

JAXB hello world example

Jakarta XML Binding (JAXB; formerly Java Architecture for XML Binding) is an XML binding framework to convert Java objects to and from XML. XML Marshalling – Convert Java objects into XML. XML Unmarshalling – Convert XML back into Java Objects. Note This article will focus on Java 11, JAXB 3 and EclipseLink MOXy JAXB RI. …

Read more

Java – How to convert Char[] to String

In Java, we can use String.valueOf() to convert a char array to a String. JavaSample1.java package com.mkyong.markdown; public class JavaSample1 { public static void main(String[] args) { char[] charArrays = new char[]{‘1’, ‘2’, ‘3’, ‘A’, ‘B’, ‘C’}; String str = new String(charArrays); System.out.println(str); // 123ABC String str2 = String.valueOf(charArrays); System.out.println(str2); // 123ABC } } Output …

Read more

Java – How to convert String to Char Array

In Java, you can use String.toCharArray() to convert a String into a char array. StringToCharArray.java package com.mkyong.utils; public class StringToCharArray { public static void main(String[] args) { String password = "password123"; char[] passwordInCharArray = password.toCharArray(); for (char temp : passwordInCharArray) { System.out.println(temp); } } } Output p a s s w o r d 1 …

Read more

java.lang.ClassNotFoundException: com.sun.mail.util.MessageRemovedIOException

Problem Use JavaMail API to send Email via GMail smtp server, but hit the following error message : Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MessageRemovedIOException at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 1 more P.S The javaee.jar library is included. Solution To solve it, you need to include the …

Read more

java.lang.ClassNotFoundException: org.objectweb.asm.Type

Problem In Hibernate development, it hits… java.lang.ClassNotFoundException: org.objectweb.asm.Type Solution This is caused by the missing of asm.jar, you can get it from asm official website, or get it via Maven <dependency> <groupId>asm</groupId> <artifactId>asm</artifactId> <version>3.3.1</version> </dependency>

How to get MAC address in Java

Since JDK 1.6, Java developers are able to access network card detail via NetworkInterface class. In this example, we show you how to get the localhost MAC address in Java. App.java – Get MAC Address via NetworkInterface.getByInetAddress() package com.mkyong; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class App{ public static void main(String[] args){ …

Read more

How to get Server IP address in Java

In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app. package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { public static void main(String[] args) { InetAddress ip; try { ip = InetAddress.getLocalHost(); System.out.println("Current IP address : " + ip.getHostAddress()); } catch (UnknownHostException e) { e.e.printStackTrace(); …

Read more

How to convert negative number to positive in Java

To convert negative number to positive number (this is called absolute value), uses Math.abs(). This Math.abs() method is work like this “number = (number < 0 ? -number : number);". See a complete example : package com.mkyong; public class app{ public static void main(String[] args) { int total = 1 + 1 + 1 + ...

Read more

Java HttpsURLConnection example

Here’s a simple Java HTTPS client to demonstrate the use of HttpsURLConnection class to send a HTTP GET request yo get the https URL content and certificate detail. P.S You may interest at this example – automate login a website with HttpsURLConnection. HttpsClient.java package com.mkyong.client; import java.net.MalformedURLException; import java.net.URL; import java.security.cert.Certificate; import java.io.*; import javax.net.ssl.HttpsURLConnection; …

Read more

How to validate URL in Java

A short example to show the use of apache.commons.validator.UrlValidator class to validate an URL in Java. import org.apache.commons.validator.UrlValidator; public class ValidateUrlExample{ public static void main(String[] args) { UrlValidator urlValidator = new UrlValidator(); //valid URL if (urlValidator.isValid("https://mkyong.com")) { System.out.println("url is valid"); } else { System.out.println("url is invalid"); } //invalid URL if (urlValidator.isValid("http://invalidURL^$&%$&^")) { System.out.println("url is valid"); …

Read more

java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5Util

Problem Validate an URL with Apache common URLValidator to validate an URL, but it hits following error message ? java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5Util at org.apache.commons.validator.UrlValidator.isValid(UrlValidator.java:242) … Caused by: java.lang.ClassNotFoundException: org.apache.oro.text.perl.Perl5Util at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1516) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1361) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) … 28 more Solution The URLValidator class is required Jakarta-ORO library, make sure you include the oro-xxx.jar into your project class …

Read more

How to convert InputStream to String in Java

This article shows a few ways to convert an java.io.InputStream to a String. Table of contents 1. ByteArrayOutputStream 2. InputStream#readAllBytes (Java 9) 3. InputStreamReader + StringBuilder 4. InputStreamReader + BufferedReader (modified line breaks) 5. Java 8 BufferedReader#lines (modified line breaks) 6. Apache Commons IO 7. Download Source Code 8. References What are modified line breaks? …

Read more