Error

warning: ‘includeantruntime’ was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds

Reason
It is caused by a misfeature introduced in Ant 1.8

Include attribute includeantruntime and set it to false as shown below

Solution 1

 <javac .... includeantruntime="false" .../>  

Solution 2
In case if your project contains many build.xml files which one cannot, or prefers not to, edit
Use presetdef add these lines in top of build.xml file

 <presetdef name="javac">
    <javac includeantruntime="false" />
  </presetdef>

If your projects do actually need ant runtime libraries, you can either add them explicitly to your build files OR set includeantruntime=”true”

<javac destdir="out" includeantruntime="true">
  <src path="foo.java" />
  <src path="bar.java" />
</javac>

Now all subsequent javac tasks will essentially inherit includeantruntime=”false”

For working with ant you need to first set the path of the environment variables.I am going to do this in command prompt by executing the following line in command prompt as follows

STEP 1
I should set the Path for Java Directory and for ant bin directory as above.

 C:\Users\Mugil>set PATH=D:\Java\jdk1.6.0_32\bin;D:\Mugil\apache-ant-1.9.0\bin;

STEP 2
Now I should create a build.xml file which the ant looks for deployment as follows
build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="default target">
 <target name="default target">
  <echo>
   Executing Default Target
  </echo>
 </target>
</project>

STEP 3
To run the ant file use the following line in command prompt as below

 d:\Mugil\staging>ant

In build.xml is a ANT Script that starts with root node project.
Project will have set of Targets
Targets will have set of Tasks

What if I named my build file to some other name other than build.xml say build2.xml
In such case you can use the below command as follows

 d:\Mugil\staging>ant -buildfile 

What if I want to run a different target

build2.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="default target">
 <target name="default target">
  <echo>
   Executing Default Target
  </echo>
 </target>
 <target name="target 2">
  <echo>
    Target 2 
  </echo>
 </target>
</project>
 d:\Mugil\staging>ant -buildfile  "target 2"

You can enforce dependency in ANT script by using depends keyword as in the following script below

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="target2">
 <target name="target1">
  <echo>
   Target 1
  </echo>
 </target>
 <target name="target2" depends="target1">
  <echo>
   Target 2
  </echo>
 </target>
</project>

When you run the ANT script you will see Target 1 and Target 2 displayed on the Screen.Though the default target is target2 since it depends on target1 both are getting displayed.

If there are two targets which depends on each other then circular dependency will occur

<?xml version="1.0" encoding="UTF-8"?>
<project name="codethataint" default="target2">
 <target name="target1" depends="target2">
 .
 .
 <target name="target2" depends="target1">
 .
 .

Creating a Folder using build.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <project name="ANT1" default="makedir"> 
 <property name="build.dir" location="D:\build"/> 
  <target name="makedir">
   <mkdir dir="${build.dir}" /> 
  </target>
 </project>

Before you are making a Folder you need to clean it as below

<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT1" default="makedir"> 
 <property name="build.dir" location="D:\build"/>
 <target name="clean">
  <delete dir="${build.dir}"/>
 </target> 
 <target name="makedir" depends="clean">
  <mkdir dir="${build.dir}" /> 
  <mkdir dir="${build.dir}/classes"/>
 </target>
</project>

Below is a Simple java project which uses an ANT script and creates three folder build.The build contains classes and jar folder.The compiled class files will sit in classes folder and jar files will sit in jar folder.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ANT1" default="makejar" basedir=".">
<property name="src.dir" location="src"/>
<property name="build.dir" location="D:\build"/>
<property name="project.name" value="ANT1"/>
<target name="clean">
 <delete dir="${build.dir}"/>
</target> 
<target name="makedir" depends="clean">  
 <mkdir dir="${build.dir}" />  
 <mkdir dir="${build.dir}/src/classes"/>
</target>
<target name="compile" depends="makedir">
  <javac srcdir="src" destdir="${build.dir}/src/classes" includeantruntime="false"/>
 </target>
<target name="makejar" depends="compile">
  <jar destfile="${build.dir}/jars/${project.name}.jar" basedir="${build.dir}/src/classes" />  
</target>
</project>

Project Java File – Sample.java

package com.mugil.tutor;

public class Sample 
{
  public static void main(String[] args) 
  {
    System.out.println("Its working...!"); 
  }
}

Once you run an ANT script it does the following task as below
1.Target Clean – Deletes the folder build
2.Target makedir – Creates two directory build and Classes
3.Target compile – Compiles code and puts in classes folder as specified in destdir
4.Target makejar – Creates the JAR file as specified in the jar tag

  • You can see in the above code src.dir is given location as src which takes the src folder relative to the folder in which the build.xml exists.
  • In the line above absolute path is used for build.dir as D:\build

Copying folder from one Location to Another

<target name="makecopy">	
  <copy todir="D:/build/web">
   <fileset dir="WebContent">
   </fileset> 
  </copy>
</target>

The above code copies the content in from WebContent folder to D:/build/web

Simple Copying of File from one directory to another

<project name="ANT2" default="copyTarget" basedir=".">
 <target name="copyTarget">
   <copy file="WebContent/index.jsp" tofile="D:/build/mugil/index2.jsp"/>
  </target>
</project>

Above I am copying a file index.jsp into location D:/build/mugil/ and saving it as index2.jsp

If you want to copy the file and Save it in to directory use the below code

 <copy file="WebContent/index.jsp" todir="D:/build/mugil/"/>

If there is no folder then folder will be created before copying file

Excluding certain type of files while copying
Now I want to exclude certain types of files from get copied say XML files.

 <target name="copyTarget">
  <copy todir="D:/build/web">
    <fileset dir="WebContent">
      <exclude name="*.xml"/>
    </fileset> 
  </copy>
 </target>

The above code excludes XML file in current folder from getting copied.Now what if i want the XML files in the sub folder from getting copied

 <target name="copyTarget">
  <copy todir="D:/build/web">
    <fileset dir="WebContent">
      <exclude name="**/*.xml"/>
    </fileset> 
  </copy>
 </target>

** is going to look for files in sub folder and excludes those from getting copied.

How to pack files into Zip

<zip destfile="D:/build/web/zip/Sample.zip">
 <fileset dir="WebContent"/>
</zip>

The above code creates a Zip file with contents in WebContent

Note:
Never hard code version numbers and folder location in build.xml as they are constantly updated over a period of time.You can create a separate file where you can put your version numbers and replace it as properties in build.xml file as shown below

build.properties
appversion=1.0

build.xml

 <project name="ANT2" default="copyTarget">
   <property file="build.properties"/>
     <target name="copyTarget">
   	    <echo>${appversion} </echo>
     </target>
 </project>

OP:1.0

Now I am going to add directory name in build.properties file as below

build.properties
dest.dir=D:\\NewFiles

The above code creates a folder in D drive with name NewFiles

Why Double Backward slash?
If you are not giving double backward slash then it will take absolute path and creates folder in project work space

Is there any other way I define my folder path?
Yes.you can define it by absolute path by using forward slash as below.

build.properties
dest.dir=D:/NewFiles

ANT properties are Immutable
Now consider the below code.

build.properties
appversion=1.1

<project name="ANT2" default="copyTarget">
  <property file="build.properties"/>
  <property name="appversion" value="1.0"/>
  <target name="copyTarget">
     <echo>${appversion} </echo>
  </target>
</project>

In line 2 I am getting the appversion value set to 1.1. Later I am trying to redefine the same property by setting the value of the attribute to 1.0.But if you print the value using echo statement you can see 1.1 getting printed on console

OP:1.1

Hence ANT Properties are immutable.

Now you want to dynamically make changes in build.properties file on fly.

 <project name="ANT2" default="copyTarget">
   <property   file="build.properties"/>
     <propertyfile   file="build.properties"  comment="My properties">
       <entry  key="appversion" value="1.3"/>
     </propertyfile>	
     <target name="copyTarget">
       <echo>${appversion} </echo>
     </target>
 </project>

In build.properties file appversion value is set to 1.1.But by using propertyfile tag i am resetting the value to 1.3.This Change will also be made in the original build.properties file.So next time when you open build.properties you can note the appversion value got changed to 1.3 and time of change as commented text.

Posted in ANT.

On Large tables dropping a column is time consuming as it needs to work on lot of records.To prevent this you can perform a logical delete of the column by Using the SET UNUSED.

ALTER TABLE table_name SET UNUSED (column_name);
ALTER TABLE table_name SET UNUSED (column_name1, column_name2);  

Once this is done the column wont be visible to users.

Later you can run a query to delete unused columns from tables

ALTER TABLE table_name DROP UNUSED COLUMNS;

In case you are working with table with less number of rows we can use drop command to drop the table column which performs Physical delete rather than using SET UNUSED which drops the column later

ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name DROP (column_name1, column_name2);

Alter table to change Column Type

ALTER TABLE emp MODIFY ename varchar2(50);

MySQL TIMEDIFF function runs well in MySQL but with JDBC it generate SQL Exception.
The problem is that the TIMEDIFF(expr1,expr2) function returns expr1 тАУ expr2 expressed as a time value.

This value is handled by java.sql.Time. But TIMEDIFF( , ) may return (for example) 12:45:00 or 40:30:01 as the case may be. For first value it works but for the second value it is not a proper time value according to java.sql.Time, hence the exception.

To get rid of this problem is to use concat as follows

CONCAT('',TIMEDIFF(expr1,expr2))

Now the returned value will be a String instead of a Time and which prevents JDBC from parsing it.

A NULL is a value for which comparison yields neither a true nor false result.NULL = NULL is not true. It’s not false either. The outcome of any comparison of one value to a NULL value is a NULL result.

If you want to check a Column for NULL value then use the following query

Query1

SELECT IFNULL(ColName, '') AS Names
  FROM TableName 

Query2

SELECT CASE WHEN ColName IS NULL
            THEN ''
            ELSE ColName
       END AS TableName 
FROM Test

Query3

 SELECT COALESCE(ColName, 0) as 'ColName' FROM TableName;

Query4

 SELECT IF(ColName IS NULL, '', ColName)
   FROM TableName 

If you already created a Table but you havent added foreign key constraint then you can do that by
running the following statement in MySQL

Run this before you run the FK Query Constraint

SET FOREIGN_KEY_CHECKS = 0; 

Run the FK Query

ALTER TABLE tbl2Name 
  ADD CONSTRAINT fkConstraint FOREIGN KEY(fkcolumnName) 
      REFERENCES tbl1Name(pkcolumnName);

Run the below code again

 SET FOREIGN_KEY_CHECKS = 1; 

Set

  • Set prevents duplication.The common use of set is to check for duplication
  • Since its is helpful for lookups for duplicate value HashSet provides an optimized implementation
  • If you want the result to be sorted use TreeSet instead of HashSet
Posted in Set.

Why ArrayList Faster than LinkedList during Random Access?
ArrayList has direct references to every element in the list, so it can get the n-th element in constant time. LinkedList has to traverse the list from the beginning to get to the n-th element.

Why LinkedList faster than ArrayList during Insertion/Deletion?
ArrayList is slower because it needs to copy part of the array in order to remove the slot that has become free. If the deletion is done using the ListIterator.remove() API, LinkedList just has to manipulate a couple of references; if the deletion is done by value or by index, LinkedList has to potentially scan the entire list first to find the element(s) to be deleted.

  • LinkedList takes constant-time insertions or removals. I can walk the list forwards or backwards, but grabbing an element in the middle takes time proportional to the size of the list.
  • ArrayLists allows random access, so I can grab any element in constant time. But adding or removing from anywhere but the end requires shifting all the latter elements over, either to make an opening or fill the gap. Also, if I add more elements than the capacity of the underlying array, a new array (twice the size) is allocated, and the old array is copied to the new one, so adding to an ArrayList is O(n) in the worst case but constant on average.
  • Iterating over both the Types is equally cheap.
  • If you have large lists, the memory usage is different. Each element of a LinkedList has more overhead since pointers to the next and previous elements are also stored

When should I use LinkedList?

  • When you need efficient removal in between elements or at the start.
  • When you don’t need random access to elements, but can live with iterating over them one by one

When should I use ArrayList?

  • When you need random access to elements (“get the nth. element”)
  • When you don’t need to remove elements from between others. It’s possible but it’s slower since the internal backing-up array needs to be reallocated.
  • Adding elements is amortized constant time (meaning every once in a while, you pay some performance, but overall adding is instantly done)
Operation Linked List Array List
Access O(n) O(1)
Insertion Access time + O(1) Access time + O(n)
Deletion Access time + O(1) Access time + O(n)