How to change Tomcat default port ?

Tomcat by default runs on port number 8080, However there is high chance get a port conflict with others program. Sometime we just need to change the Tomcat port number. Steps of changing the Tomcat Port 1) Locate server.xml in {Tomcat installation folder}\ conf \ 2) Find following similar statement <!– Define a non-SSL HTTP/1.1 …

Read more

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

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 download from Maven remote repository?

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

Where is Maven Central 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

Java’s silent killer – Integer Overflow, Careful !

Believe it or not, Java contains Integer buffer overflow as well. I’m not sure is this the correct word to describe it or not, may be you can suggest some 🙂 OK, please take a look at below program, this program print the number of microseconds in a day. public class JavaLongOverflow { public static …

Read more

Different between Inline and Attachment in Email

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

How to calculate monetary values in Java

There are many monetary values calculation in the financial or e-commerce application, and there is one question that arises for this – Should we use double or float data type to represent the monetary values? Answer: Always uses java.math.BigDecimal to represent the monetary values. 1. Double or Float? Here is an example of using double …

Read more

How to get time in milliseconds in Java

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 calculate elapsed / execute time 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 loop / iterate a List in Java

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 convert Array to 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

Where is Maven local repository?

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

How to check the CPU temperature ?

Often time , i like to know my computer, motherboard or CPU temperature. It’s will be better if some utility tools provide the functionality to show all my processors or CPU temperature ~ Here is the two free tools that i used to check my CPU temperature Core Temp Software Name : Core Temp Website …

Read more

Connect to PostgreSQL with JDBC driver

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

SWT – How to capture keyboard event

In SWT, keyboard event is represent by KeyEvent class. We can use KeyListener to receive and process a KeyEvent. The KeyEvent class has three member fields to provide information about the key that generated by event. 1) character – Display a char value of the pressed key. 2) stateMask – Check whether any other keys …

Read more

Where to download SWT Source Code?

The Standard Widget Toolkit (SWT) is an open source project. However Eclipse SWT plugin does not bundle with SWT source code. We have to manually download it ~ Here is the steps to download the SWT Source Code 1) Visit the Standard Widget Toolkit (SWT) Official Website http://www.eclipse.org/swt/ 2) Under Stable, select your operating system …

Read more

SWT – MouseListener & MouseAdapter Example

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

Bind variables Performance Test in Java

I heard a lot of people talking about “Bind variables” will increase java application performance. Is this really true? i skeptical and make a simple performance test between Bind variables in PreparedStatement class and Non Bind variables in Statement class How do i test it? I will create a simple java class and keep sending …

Read more

SWT – TabFolder 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 – SashForm 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 – Group 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 – Button 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 – Label 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 Positioning – setBounds() or setLocation()

When we start learn about SWT GUI programming, we always want to figure out how do we positioning the Text field, Label, button and other widget. In SWT, we can use setLocation() or setLocation() method to specify the size and position of a widget or component. Here is the two methods that SWT use to …

Read more

SWT Hello World 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

How to import SWT library into Eclipse Workspace?

In order to develop SWT application in Eclipse environment, we have to import SWT library into Eclipse Workspace manually. Here is the steps to import SWT library 1) Right click on Java Project – Click Properties 2) Click Java Build Path –> Libraries –> Add Variable –> Configure Variable 3) Click New –> Type a …

Read more

How to modify date time (Date Manipulation) – Java

Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class. Get current date time with Calendar() DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println("Current Date Time : " + dateFormat.format(cal.getTime())); Calender date time manipulation …

Read more

How to display hibernate sql parameter values – P6Spy

Question There are many developers asking about Hibernate SQL parameter value question. How to display the Hibernate SQL parameter values that passed to database? Hibernate just display all parameter values as question mark (?). With show_sql property, Hibernate will shows all generated SQL statements, but not the SQL parameter values. For example Hibernate: insert into …

Read more

Remember that ordinal parameters are 1-based! – HibernateTemplate

Problem HibernateTemplate code … getHibernateTemplate().find("from Domain d where d.domainName = :domainName", domainName); When i execute the above code, i hit the following error message java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! … at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:397) at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:369) Solution I go inside and study HibernateTemplate.java file and find below code public List find(final String queryString, final Object[] …

Read more

Java – How to read input from the console

In Java, there are three ways to read input from a console : System.console (JDK 1.6) Scanner (JDK 1.5) BufferedReader + InputStreamReader (Classic) 1. System.console Since JDK 1.6, the developer starts to switch to the more simple and powerful java.io.Console class. JavaConsole.java package com.mkyong.io; import java.io.Console; public class JavaConsole { public static void main(String[] args) …

Read more