Wednesday, January 18, 2012

The specified JRE installation does not exist

This error occurs when the jre specified in the ant run time configuration does not exist on the system. When we try to run the ant build file, it throws the following error.


Opening the details of the error shows that the ant build failed due to a coonflicted version of the jre.



In order to remove this error, you need to right click on the ant file. In the Run As you need to click on Open External Tools Dialog as shown in figure.



In the dialog box, under the Ant Build, select the ant build file you need to set the JRE for. Open the tab named JRE, probably at the extreme right of the dialog box. And select the JRE which you are using for development.






How to change server location in Eclipse



The radio buttons for changing location in server configuration file seem disabled. I loved myEclipse over Eclipse only due to this problem in Eclipse. The problem arises when we add or publish a project to a server in Eclipse. The screenshot of disabled location buttons is given as



There are three solutions to this problem.
  1. Server created but no projects published.
  2. Server Created and projects published into it.
    1. Change location in server properties
    2. Change location after removing projects.

  1. Server created but no projects published.
When a server is created and projects are added into it, DO NOT PUBLISH any projects into the server and you will be able to change the server location with the radio buttons.
  1. Server Created and projects published into it.
When a server is created and projects are added into it and we publish projects into the server, the location button in server configuration file go disabled. In these cases, in order to change the location, we must follow either of the following two methods
    1. Change location in server properties
Right click on the server, go to properties, go to general, you will see a change location button as seen in the screenshot.

 Press it and you will see a changed location left to it. You can either apply the changes directly from here or press the Cancel button and go to the server configuration file where you will see the enabled location radio buttons now.  
    1. Change location after removing projects.
Right click on server, click on Add and Remove…, remove all the projects. Then right click on the server and click Clean….
    
 




Now you will be able to see the enabled location changing buttons in the server configuration file.








Thursday, December 22, 2011

How to implement a TreeMap


java.util.TreeMap is an implementation of the java.util.SortedMap interface. This class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used.

Following is a simple example of inserting employees into a map based on their countries. The example consists of 2 classes.

Employee.java

package com.soft.model;

public class Employee{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}

}

TreeMapExample.java

package com.soft.examples;

import java.util.Collections;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import com.soft.model.Employee;

public class TreeMapExample {
            public static void main(String[] args) {
SortedMap<String, Employee> map = new TreeMap<String, Employee>();
SortedMap<String, Employee> descendingMap = new TreeMap<String, Employee>(Collections.reverseOrder());
Employee e = new Employee();
e.setName("Danial Fritz");
e.setDepartment("Field Based Development");
e.setAge(36);
map.put("Canada", e);
descendingMap.put("Canada", e);
e = new Employee();
e.setName("Denis Khoo");
e.setDepartment("Project Management");
e.setAge(30);
map.put("England", e);
descendingMap.put("England", e);
e = new Employee();
e.setName("Joe Kum");
e.setDepartment("Adminstration");
e.setAge(40);
map.put("Pakistan", e);
descendingMap.put("Pakistan", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(26);
map.put("China", e);
descendingMap.put("China", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(26);
map.put("Brazil", e);
descendingMap.put("Brazil", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Information Technology");
e.setAge(25);
map.put("Kowait", e);
descendingMap.put("Kowait", e);
e = new Employee();
e.setName("Anil Kumar");
e.setDepartment("Project Management");
e.setAge(26);
map.put("Jordan", e);
descendingMap.put("Jordan", e);
System.out.println("************ Sorting By Natural Order of keys ***********");
Set<String> keySet = map.keySet();
for (String key : keySet) {
            Employee employee = map.get(key);
System.out.println(key + "-----" + employee.getName() + "-----"+ employee.getDepartment() + "-----" + employee.getAge());
}
System.out.println("*** Descending Sort By Natural Order of keys ***");
keySet = descendingMap.keySet();
for (String key : keySet) {
Employee employee = map.get(key);
System.out.println(key + "-----" + employee.getName() + "-----"
+ employee.getDepartment() + "-----" + employee.getAge());
}
}
}


Output:
************ Sorting By Natural Order of keys ***********

Brazil-----Anil Kumar-----Information Technology-----26
Canada-----Danial Fritz-----Field Based Development-----36
China-----Anil Kumar-----Information Technology-----26
England-----Denis Khoo-----Project Management-----30
Jordan-----Anil Kumar-----Project Management-----26
Kowait-----Anil Kumar-----Information Technology-----25
Pakistan-----Joe Kum-----Adminstration-----40

*** Descending Sort By Natural Order of keys ***

Pakistan-----Joe Kum-----Adminstration-----40
Kowait-----Anil Kumar-----Information Technology-----25
Jordan-----Anil Kumar-----Project Management-----26
England-----Denis Khoo-----Project Management-----30
China-----Anil Kumar-----Information Technology-----26
Canada-----Danial Fritz-----Field Based Development-----36
Brazil-----Anil Kumar-----Information Technology-----26

Wednesday, December 21, 2011

How to implement ReverseOrderComparable

Java.Util.Comparable is an interface used for sorting collections. In previous example, we used a MultyField Comparable to sort a list in ascending order by multiple fields. In this example, we will implement a reverse order multy field Comparable in order to achieve a sorting in descending order or in other words in reverse order of whatever is implemented in the Comparable. This can be achieved just by a single line of code as
Comparator c= Collections.reverseOrder();// yes a comparator is used to achieve it.
This example is almost a replica of the previous example. It consists of the following 2 classes.

Employee.java:

package com.soft.model;

public class Employee{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}
@Override
public int compareTo(Object obj) {
            Employee employee=(Employee)obj;
            if(this.getName().compareTo(employee.getName())==0){
                        if(this.getDepartment().compareTo(employee.getDepartment())==0){
                                    return this.getAge()-employee.getAge();
                        }else{
                                    return this.getDepartment().compareTo(employee.getDepartment());
                        }
            }else{
                        return this.getName().compareTo(employee.getName());
            }
}
}


MultyFieldComparableExample.java:

package com.soft.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.soft.model.Employee;

public class MultyFieldComparableExample {
            public static void main(String[] args) {
                        List<Employee> list= new ArrayList<Employee>();
                        Employee e=new Employee();
                        e.setName("Danial Fritz");
                        e.setDepartment("Field Based Development");
                        e.setAge(36);
                        list.add(e);
                        e=new Employee();
                        e.setName("Denis Khoo");
                        e.setDepartment("Project Management");
                        e.setAge(30);
                        list.add(e);
                        e=new Employee();
                        e.setName("Joe Kum");
                        e.setDepartment("Adminstration");
                        e.setAge(40);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(25);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Project Management");
                        e.setAge(26);
                        list.add(e);
                        System.out.println("************ Before Sorting ***********");
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                        System.out.println("**Name, Department, Age Sorting through Comparable **");
                        Collections.sort(list);
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }

System.out.println("** Name, Department, Age Sorting through Comparable In Descending Order**");
Collections.sort(list,Collections.reverseOrder());
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
                       
            }
}

Output:
************ Before Sorting ***********
Danial Fritz   ----- Field Based Development   ----- 36
Denis Khoo     ----- Project Management        ----- 30
Joe Kum        ----- Adminstration             ----- 40
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 25
Anil Kumar-----Project Management-----26

** Name, Department, Age Sorting through Comparable **

Anil Kumar     ----- Information Technology     ----- 25
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Project Management         ----- 26
Danial Fritz   ----- Field Based Development    ----- 36
Denis Khoo     ----- Project Management         ----- 30
Joe Kum        ----- Adminstration              ----- 40

** Name, Department, Age Sorting through Comparable in Descending Order**

Joe Kum        ----- Adminstration                     ----- 40
Denis Khoo   ----- Project Management           ----- 30
Danial Fritz   ----- Field Based Development   ----- 36
Anil Kumar   ----- Project Management           ----- 26
Anil Kumar   ----- Information Technology     ----- 26
Anil Kumar   ----- Information Technology     ----- 26
Anil Kumar   ----- Information Technology     ----- 25

An example of a simple MultyField Comparable can be found at
You may also find the following articles helpful.

How to implement ReverseOrderComparator


Java.Util.Comparator is an interface used for sorting collections. In previous example, we used a MultyField Comparator to sort a list in ascending order by multiple fields. In this example, we will implement a reverse order multy field Comparator in order to achieve a sorting in descending order or in other words in reverse order of whatever is implemented in the Comparator. This can be achieved just by a single line of code as
Comparator c= Collections.reverseOrder(new mployeeMultyFieldComparator());
 This example is almost a replica of the previous example. It consists of the following 3 classes.

Employee.java:

package com.soft.model;

public class Employee{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}

}



EmployeeMultyFieldComparator.java:

package com.soft.model;

import java.util.Comparator;

public class EmployeeMultyFieldComparator implements Comparator{

            @Override
            public int compare(Object obj1, Object obj2) {
                        Employee e1=(Employee)obj1;
                        Employee e2=(Employee)obj2;
                        if(e1.getName().compareTo(e2.getName())==0){
                                    if(e1.getDepartment().compareTo(e2.getDepartment())==0){
                                                return e1.getAge()-e2.getAge();
                                    }else{
                                                return e1.getDepartment().compareTo(e2.getDepartment());
                                    }
                        }else{
                                    return e1.getName().compareTo(e2.getName());
                        }
            }

}


MultyFieldComparatorExample.java:

package com.soft.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.soft.model.Employee;
import com.soft.model.EmployeeMultyFieldComparator;

public class MultyFieldComparatorExample {
            public static void main(String[] args) {
                        List<Employee> list= new ArrayList<Employee>();
                        Employee e=new Employee();
                        e.setName("Danial Fritz");
                        e.setDepartment("Field Based Development");
                        e.setAge(36);
                        list.add(e);
                        e=new Employee();
                        e.setName("Denis Khoo");
                        e.setDepartment("Project Management");
                        e.setAge(30);
                        list.add(e);
                        e=new Employee();
                        e.setName("Joe Kum");
                        e.setDepartment("Adminstration");
                        e.setAge(40);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(25);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Project Management");
                        e.setAge(26);
                        list.add(e);
                        System.out.println("************ Before Sorting ***********");
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                        System.out.println("************ Name, Department, Age Sorting through Comparator ***********");
                        Collections.sort(list,new EmployeeMultyFieldComparator());
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }

System.out.println("** Name, Department, Age Sorting through Comparator In Descending Order**");
Collections.sort(list,Collections.reverseOrder(new EmployeeMultyFieldComparator()));
for (int i=0;i< list.size();i++){
System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
}
                       
            }
}

Output:
************ Before Sorting ***********
Danial Fritz   ----- Field Based Development   ----- 36
Denis Khoo     ----- Project Management        ----- 30
Joe Kum        ----- Adminstration             ----- 40
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 25
Anil Kumar-----Project Management-----26

****** Name, Department, Age Sorting through Comparator *******

Anil Kumar     ----- Information Technology     ----- 25
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Project Management         ----- 26
Danial Fritz   ----- Field Based Development    ----- 36
Denis Khoo     ----- Project Management         ----- 30
Joe Kum        ----- Adminstration              ----- 40

** Name, Department, Age Sorting through Comparator in Descending Order**

Joe Kum        ----- Adminstration                     ----- 40
Denis Khoo   ----- Project Management           ----- 30
Danial Fritz   ----- Field Based Development   ----- 36
Anil Kumar   ----- Project Management           ----- 26
Anil Kumar   ----- Information Technology     ----- 26
Anil Kumar   ----- Information Technology     ----- 26
Anil Kumar   ----- Information Technology     ----- 25

An example of a simple MultyFieldComparator can be found at
You may also find the following articles helpful.


Tuesday, December 20, 2011

How to implement MultyFieldComparable

Java.Util.Comparable is an interface used for sorting collections. In previous example, we used Comparable for sorting by only one field. In this example, we will see a more complex Comparable which can be used to sort a list of employees based on the name, then on department and then on age. The example consists of the following 2 classes.

Employee.java:

package com.soft.model;

public class Employee implements Comparable{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}
@Override
public int compareTo(Object obj) {
            Employee employee=(Employee)obj;
            if(this.getName().compareTo(employee.getName())==0){
                        if(this.getDepartment().compareTo(employee.getDepartment())==0){
                                    return this.getAge()-employee.getAge();
                        }else{
                                    return this.getDepartment().compareTo(employee.getDepartment());
                        }
            }else{
                        return this.getName().compareTo(employee.getName());
            }
}

}



MultyFieldComparableExample.java:

package com.soft.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.soft.model.Employee;

public class MultyFieldComparableExample {
            public static void main(String[] args) {
                        List<Employee> list= new ArrayList<Employee>();
                        Employee e=new Employee();
                        e.setName("Danial Fritz");
                        e.setDepartment("Field Based Development");
                        e.setAge(36);
                        list.add(e);
                        e=new Employee();
                        e.setName("Denis Khoo");
                        e.setDepartment("Project Management");
                        e.setAge(30);
                        list.add(e);
                        e=new Employee();
                        e.setName("Joe Kum");
                        e.setDepartment("Adminstration");
                        e.setAge(40);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(25);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Project Management");
                        e.setAge(26);
                        list.add(e);
                        System.out.println("************ Before Sorting ***********");
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                        System.out.println("************ Name, Department, Age Sorting through Comparable ***********");
                        Collections.sort(list);
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                       
            }
}


Output:

************ Before Sorting ***********
Danial Fritz   ----- Field Based Development   ----- 36
Denis Khoo     ----- Project Management        ----- 30
Joe Kum        ----- Adminstration             ----- 40
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 25
Anil Kumar-----Project Management-----26

****** Name, Department, Age Sorting through Comparable *******

Anil Kumar     ----- Information Technology     ----- 25
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Project Management         ----- 26
Danial Fritz   ----- Field Based Development    ----- 36
Denis Khoo     ----- Project Management         ----- 30
Joe Kum        ----- Adminstration              ----- 40

An example of a MultyFieldComparator can be found at
An example of a simple Comparable can be found at
You may also find the following articles helpful.




_________________________________________________________________________

How to implement MultyFieldComparator

Java.Util.Comparator is an interface used for sorting collections. In previous example, we used comparator for sorting by only one field. In this example, we will see a more complex comparator which can be used to sort a list of employees based on the name, then on department and then on age. The example consists of the following 3 classes.

Employee.java:

package com.soft.model;

public class Employee{
private String name;
private String department;
private int age;
public String getName() {
            return name;
}
public void setName(String name) {
            this.name = name;
}
public String getDepartment() {
            return department;
}
public void setDepartment(String department) {
            this.department = department;
}
public int getAge() {
            return age;
}
public void setAge(int age) {
            this.age = age;
}

}



EmployeeMultyFieldComparator.java:

package com.soft.model;

import java.util.Comparator;

public class EmployeeMultyFieldComparator implements Comparator{

            @Override
            public int compare(Object obj1, Object obj2) {
                        Employee e1=(Employee)obj1;
                        Employee e2=(Employee)obj2;
                        if(e1.getName().compareTo(e2.getName())==0){
                                    if(e1.getDepartment().compareTo(e2.getDepartment())==0){
                                                return e1.getAge()-e2.getAge();
                                    }else{
                                                return e1.getDepartment().compareTo(e2.getDepartment());
                                    }
                        }else{
                                    return e1.getName().compareTo(e2.getName());
                        }
            }

}


MultyFieldComparatorExample.java:

package com.soft.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.soft.model.Employee;
import com.soft.model.EmployeeMultyFieldComparator;

public class MultyFieldComparatorExample {
            public static void main(String[] args) {
                        List<Employee> list= new ArrayList<Employee>();
                        Employee e=new Employee();
                        e.setName("Danial Fritz");
                        e.setDepartment("Field Based Development");
                        e.setAge(36);
                        list.add(e);
                        e=new Employee();
                        e.setName("Denis Khoo");
                        e.setDepartment("Project Management");
                        e.setAge(30);
                        list.add(e);
                        e=new Employee();
                        e.setName("Joe Kum");
                        e.setDepartment("Adminstration");
                        e.setAge(40);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(26);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Information Technology");
                        e.setAge(25);
                        list.add(e);
                        e=new Employee();
                        e.setName("Anil Kumar");
                        e.setDepartment("Project Management");
                        e.setAge(26);
                        list.add(e);
                        System.out.println("************ Before Sorting ***********");
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                        System.out.println("************ Name, Department, Age Sorting through Comparator ***********");
                        Collections.sort(list,new EmployeeMultyFieldComparator());
                        for (int i=0;i< list.size();i++){
                                    System.out.println(list.get(i).getName()+"-----"+list.get(i).getDepartment()+"-----"+list.get(i).getAge());
                        }
                       
            }
}

Output:
************ Before Sorting ***********
Danial Fritz   ----- Field Based Development   ----- 36
Denis Khoo     ----- Project Management        ----- 30
Joe Kum        ----- Adminstration             ----- 40
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 26
Anil Kumar     ----- Information Technology    ----- 25
Anil Kumar-----Project Management-----26

****** Name, Department, Age Sorting through Comparator *******

Anil Kumar     ----- Information Technology     ----- 25
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Information Technology     ----- 26
Anil Kumar     ----- Project Management         ----- 26
Danial Fritz   ----- Field Based Development    ----- 36
Denis Khoo     ----- Project Management         ----- 30
Joe Kum        ----- Adminstration              ----- 40

An example of a simple Comparator can be found at

You may also find the following articles helpful.