How to convert Date in BeanWrapperFieldSetMapper

Read following Spring batch job, it reads data from “domain.csv“, and map it to a domain object. job-example.xml <bean class="org.springframework.batch.item.file.FlatFileItemReader" > <property name="resource" value="file:outputs/csv/domain.csv" /> <property name="lineMapper"> <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="lineTokenizer"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="id, domainName, lastModifiedDate" /> </bean> </property> <property name="fieldSetMapper"> <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="domain" /> </bean> </property> </bean> </property> </bean> <bean …

Read more

How to add copyright to Eclipse automatically

A quick guide to show you how to add @Copyright comment to new and existing Java files in Eclipse IDE. Solution 1. Eclipse Code Templates Go to preferences -> Java -> Code Style -> Code Templates, expands Code and select “New Java Files”, edit the template to add whatever copyright messages you want. Now, that …

Read more

jobParameters cannot be found on object of type BeanExpressionContext

Create a simple Spring batch job to write data to a csv file. The csv file name depends on the pass in job’s parameters, interprets by Spring EL . job-sample.xml <bean id="csvFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"> <!– write to this csv file –> <property name="resource" value="file:outputs/csv/domain.done.#{jobParameters[‘pid’]}.csv" /> <property name="appendAllowed" value="false" /> <property name="lineAggregator"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"> <property name="delimiter" value="," …

Read more

How to find Java class in Eclipse?

In Eclipse IDE, you can type CTRL + SHIFT + T in Windows or *nix or Command + SHIFT + T in Mac OSX to prompt an Open Type dialog box to find details about a specified Java class. For example, if you want to know the detail of this Java class – FlatFileItemWriter (Spring …

Read more

NoSuchBeanDefinitionException : No qualifying bean of type JobLauncherTestUtils

Following the official Spring batch unit testing guide to create a standard unit test case. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:spring/batch/jobs/job-abc.xml", "classpath:spring/batch/config/context.xml"}) public class AppTest { @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Test public void launchJob() throws Exception { JobExecution jobExecution = jobLauncherTestUtils.launchJob(); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); } } P.S spring-batch-test.jar is added to the classpath. Problem When launches above …

Read more

Spring Batch unit test example

In this tutorial, we will show you how to unit test Spring batch jobs with jUnit and TestNG frameworks. To unit test batch job, declares spring-batch-test.jar, @autowired the JobLauncherTestUtils, launch the job or step, and assert the execution status. 1. Unit Test Dependencies To unit test Spring batch, declares following dependencies : pom.xml <!– Spring …

Read more

How to convert String to Date – Java

In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways. // String -> Date SimpleDateFormat.parse(String); // Date -> String SimpleDateFormat.format(date); Refer to table below for some of the common date and time …

Read more

Convert String with commas to Long – Java

A short guide to show you how to convert a String with commas to a long type. 1. For a normal String, you can use Long.valueOf to convert it directly. String bigNumber = "1234567899"; long result = Long.valueOf(bigNumber); 2. For a String with commas, you can use java.text.NumberFormat to convert it. String bigNumber = "1,234,567,899"; …

Read more

Spring Batch metadata tables are not created automatically?

If jobRepository is created with MapJobRepositoryFactoryBean (metadata in memory), the Spring batch jobs are running successfully. spring-config.xml <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager" /> </bean> Problem After changing the jobRepository to store metadata into database : spring-config.xml <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="databaseType" value="mysql" /> </bean> <bean id="jobLauncher" …

Read more

Spring Batch : A job instance already exists and is complete for parameters={}

Working with Spring Batch 2.2.0.RELEASE, and launches the job with Spring Scheduler. CustomJobLauncher.java package com.mkyong.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class CustomJobLauncher { @Autowired JobLauncher jobLauncher; @Autowired Job job; public void run() { try { JobExecution execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + …

Read more

Java Whois example

In this tutorial, we will show you how to use Java library – “Apache Commons Net” to get WHOIS data of a domain. 1. Simple Java Whois Example For domain registered under internic.net, you can get the whois data directly. WhoisTest.java package com.mkyong.whois.bo; import java.io.IOException; import java.net.SocketException; import org.apache.commons.net.whois.WhoisClient; public class WhoisTest { public static …

Read more

How to send HTTP request GET/POST in Java

In this article, we will show you a few examples to make HTTP GET/POST requests via the following APIs Apache HttpClient 4.5.10 OkHttp 4.2.2 Java 11 HttpClient Java 1.1 HttpURLConnection (Not recommend) 1. Apache HttpClient In the old days, this Apache HttpClient is the de facto standard to send an HTTP GET/POST request in Java. …

Read more

Apache HttpClient Examples

This article shows you how to use Apache HttpClient to send an HTTP GET/POST requests, JSON, authentication, timeout, redirection and some frequent used examples. P.S Tested with HttpClient 4.5.10 pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.10</version> </dependency> 1. Send GET Request 1.1 Close manually. HttpClientExample1_1.java package com.mkyong.http; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; …

Read more

According to TLD, tag form:input must be empty, but is not

Problem Developing a search form with the Spring MVC framework. /WEB-INF/pages/tools/webserver.jsp – Spring mvc + form tag <form:form method="post" commandName="searchForm" action="${pageContext.request.contextPath}/tools/webserver/" class="navbar-form pull-left" id="search-form"> Type a website : <form:input path="domainName" type="text" width="165px" placeholder="example – google.com" /> <button type="submit" class="btn btn-top-margin">Search</button> </form:form> Spring Controller @Controller @RequestMapping(value = "/tools", method = RequestMethod.GET) public class ToolsController { @RequestMapping(value …

Read more

ASCII Art Java example

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text. P.S This example is credited for this post ASCIIArtService.java package com.mkyong.service; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; public class ASCIIArtService { public static void main(String[] …

Read more

New Relic for PHP, with cPanel + VPS

Here’s my journey to install “New Relic for PHP” to monitor my WordPress’s blog performance. Below is my server environment : Operating system CentOS 6.x 64-bit, VPS with root access cPanel 11.x Apache version 2.x PHP version 5.3.13 WordPress 3.5.1 P.S The New Relic is a web application performance tool. First, see how “New Relic …

Read more

Spring asm dependency issue in Spring Data

Using Spring Data MongoDB 1.2.1.RELEASE and Spring core 3.2.2.RELEASE, while system is starting, it hits some weird spring-asm IncompatibleClassChangeError errors : java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class pom.xml <properties> <spring.version>3.2.2.RELEASE</spring.version> <springdata.version>1.2.1.RELEASE</springdata.version> </properties> <dependencies> <!– Spring Core –> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!– Spring Data for MongoDB –> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${springdata.version}</version> …

Read more

Display a list of countries in Java

In this article, we show you how to use the Locale class to play around the list of countries. P.S Tested with JDK 1.6 1. List of Countries The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166. ListCountry.java package com.webmitta.model; import java.util.Locale; public class ListCountry { public static void …

Read more

MongoDB import and export example

In this tutorial, we show you how to backup and restore MongoDB with the commands : mongoexport and mongoimport. 1. Backup database with mongoexport Few examples to show you how to use the mongoexport to back up the database. Review some of the common use options. $ mongoexport Export MongoDB data to CSV, TSV or …

Read more

Spring Data MongoDB : Save binary file, GridFS example

In MongoDB, you can use GridFS to store binary files. In this tutorial, we show you how to use Spring Data’s GridFsTemplate to store / read image in / from MongoDB. 1. GridFS – Save example (Spring config in Annotation) Gat an image file and save it into MongoDB. SpringMongoConfig.java package com.mkyong.config; import org.springframework.context.annotation.Bean; import …

Read more

Java HttpURLConnection follow redirect example

The HttpURLConnection‘s follow redirect is just an indicator, in fact it won’t help you to do the “real” http redirection, you still need to handle it manually. URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully. HttpURLConnection.setFollowRedirects(true); 1. Java Http Redirect Example If a server is …

Read more

Download JDK Source code for Mac OS X

The JDK source code is packaged in a src.jar, and should be in the JDK/Home folder. However, some JDK versions in Mac OSX didn’t include the source code or Javadoc. Try find it : sudo find / -name src.jar If you couldn’t find the src.jar, then get it from Apple developer website. 1. Download from …

Read more

How to get HTTP Response Header in Java

This example shows you how to get the Http response header values in Java. 1. Standard JDK example. URL obj = new URL("https://mkyong.com"); URLConnection conn = obj.openConnection(); //get all headers Map<String, List<String>> map = conn.getHeaderFields(); for (Map.Entry<String, List<String>> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue()); } …

Read more

MongoDB Authentication example

This guide shows you how to enable authentication in MongoDB. The authentication is disabled by default. To configure it, you must first add a user to the “admin” database. > show dbs admin #add single user to this database testdb Note Users with normal access in “admin” database, HAVE read and write access to all …

Read more

MongoDB : couldn’t open /data/db/yourdb.ns errno:13 Permission denied

Starting MongoDB server, it shows error “Permission denied” on one of the database and shutdown the server automatically. $ mongod Fri Mar 8 22:54:46 [initandlisten] MongoDB starting : pid=13492 port=27017 dbpath=/data/db/ 64-bit host=Yongs-MacBook-Air.local //… Fri Mar 8 22:54:46 [initandlisten] journal dir=/data/db/journal Fri Mar 8 22:54:46 [initandlisten] recover : no journal files present, no recovery needed …

Read more

How to install MongoDB on Mac OS X

A guide to show you how to install MongoDB on Mac OS X. MongoDB 2.2.3 Mac OS X 10.8.2 1. Download MongoDB Get MongoDB from official website, extracts it : $ cd ~/Download $ tar xzf mongodb-osx-x86_64-2.2.3.tgz $ sudo mv mongodb-osx-x86_64-2.2.3 /usr/local/mongodb 2. MongoDB Data By default, MongoDB write/store data into the /data/db folder, you …

Read more

How to set environment variables on Mac OS X

In Mac OS X, you can set the environment variables in one of the following files : ~/.bashrc ~/.bash_profile ~/.profile By default, Mac OS X does not has above files, you need to create it manually. $PATH example This example shows you how to set “mongodb/bin” folder to the existing $PATH environment variable. $ echo …

Read more

Maven $JAVA_HOME is not defined correctly on Mac OS

This article shows how to fix the Maven error JAVA_HOME is not defined correctly. Terminal $ mvn -version Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java Further Reading How to set $JAVA_HOME environment variable on macOS 1. $JAVA_HOME and macOS 10.15 Catalina, macOS 11 Big Sur On macOS 10.15 Catalina and later, the …

Read more

Due to limitations of the BasicDBObject, you can’t add a second ‘$and’

Problem Using Spring data and Mongodb, below is a function to find data within a date range. public List<RequestAudit> findByIpAndDate(String ip, Date startDate, Date endDate) { Query query = new Query( Criteria.where("ip").is(ip) .andOperator(Criteria.where("createdDate").gte(startDate)) .andOperator(Criteria.where("createdDate").lt(endDate)) ); return mongoOperation.find(query, RequestAudit.class); } It hits following error message : org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can’t add …

Read more

How to Set $JAVA_HOME environment variable on macOS

This article shows how to set the $JAVA_HOME environment variable on older Mac OS X and the latest macOS 11. Topics macOS release history What is /usr/libexec/java_home $JAVA_HOME and macOS 11 Big Sur $JAVA_HOME and Mac OS X 10.5 Leopard $JAVA_HOME and older Mac OS X Switch between different JDK versions Solution Steps to set …

Read more

How to get client Ip Address in Java

In Java, you can use HttpServletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application. import javax.servlet.http.HttpServletRequest; String ipAddress = request.getRemoteAddr(); 1. Proxy Server or Cloudflare For web application which is behind a proxy server, load balancer or the popular Cloudflare solution, you should get the client IP address via the HTTP …

Read more

Access restriction: The type BASE64Encoder is not accessible due to restriction

Problem Downloaded a Java sample from Alexa API on Amazon service, imports it into Eclipse, but unable to compile and hits following “Access restriction” errors : Access restriction: The type BASE64Encoder is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar P.S Using JDK 1.6 and Eclipse IDE 4.2 import sun.misc.BASE64Encoder; // Access restriction error …

Read more

Spring REST @RequestMapping extract incorrectly if value contains ‘.’

Problem See following Spring REST example, if a request such as “http://localhost:8080/site/google.com” is submitted, Spring returns “google“. Look like Spring treats “.” as file extension, and extract half of the parameter value. SiteController.java package com.mkyong.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/site") public class SiteController { @RequestMapping(value = "/{domain}", method …

Read more