Monday, 14 May 2012

Problem: failed to create task or type antlib:org.apache.ivy.ant:retrieve

I've just installed NetBeans 7.1.2. When attempting to run an Ivy project I get the following error:

/home/rsharp/projects/project_name/ant-ivy.xml:5: Problem: failed to create task or type antlib:org.apache.ivy.ant:retrieve
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet
This appears to be an antlib declaration.
Action: Check that the implementing library exists in one of:
        -ANT_HOME/lib
        -the IDE Ant configuration dialogs
BUILD FAILED (total time: 0 seconds)

This was because the default location for Ant in NetBeans is:

/usr/local/netbeans-7.1.2/java/ant

Whereas Ant is actually installed at:

/usr/local/ant

To set this, go to Tools > Options > Miscellaneous > Ant and modify Ant Home.

Thursday, 3 May 2012

DriverManager.setLoginTimeout() and DriverManager.getLoginTimeout()


When attempting to connect to a data source which is unreliable, to stop the application from hanging (i.e. waiting for a response from the DriverManager,getConnection(...) method), you can use the login timeout integer. This represents a maximum time in seconds that the data source will wait when attempting to connect to the database.

Description from Oracle Java Docs:

DriverManager.setLoginTimeout ( )  - Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A value of zero specifies that the timeout is the default system timeout if there is one; otherwise, it specifies that there is no timeout. When a DataSource object is created, the login timeout is initially zero.

DriverManager.getLoginTimeout( )  - Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A value of zero means that the timeout is the default system timeout if there is one; otherwise, it means that there is no timeout. When a DataSource object is created, the login timeout is initially zero. 

Wednesday, 11 April 2012

Java bubble sort with ArrayList and custom args

Bubble sort: http://en.wikipedia.org/wiki/Bubble_sort

As a bit of fun I wrote my own version of the bubble sort algorithm.

package bubblesort;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Java bubble sort example: This class shows how to sort an array list of
 * Integers using the bubble sort algorithm. Enter numbers as arguments to be
 * sorted.
 *
 * @author rsharp 9/4/12
 */
public class BubbleSort {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        if (args.length > 0) {

            ArrayList<Integer> numbersToSort = new ArrayList<Integer>();

            for (String arg : args) {
                try {
                    numbersToSort.add(Integer.parseInt(arg));
                } catch (NumberFormatException ex) {
                    System.out.println(arg + " is not a number. Please enter only numbers into the bubble sort!");
                }
            }

            //print array before sorting using bubble sort algorithm
            System.out.println("Array before the bubble sort");
            for (int i : numbersToSort) {
                System.out.print(i + " ");
            }

            //sort an array using bubble sort algorithm
            bubbleSort(numbersToSort);

            System.out.println("");

            //print array after sorting using bubble sort algorithm
            System.out.println("Array after bubble sort");
            for (int i : numbersToSort) {
                System.out.print(i + " ");
            }

        } else {
            System.out.println("Please enter a list of numbers to bubble sort!");
        }

    }

    /**
     * This method performs the bubble sort
     * @param numbersToSort
     */
    private static void bubbleSort(ArrayList<Integer> numbersToSort) {

        int temp = 0;
        for (int i = 0; i < numbersToSort.size() - 1; i++) {
            for (int j = 1; j < (numbersToSort.size() - i); j++) {
                if (numbersToSort.get(j - 1) > numbersToSort.get(j)) {
                    //swap the elements!
                    temp = numbersToSort.get(j - 1);
                    numbersToSort.set(j - 1, numbersToSort.get(j));
                    numbersToSort.set(j, temp);
                }

            }
        }
    }
}

You can run this from the command line with arguments. There is error checking for if no or incorrect arguments are sent in:

java bubblesort.BubbleSort 10 2 7 8 3 1 6 9 5 4

Tuesday, 10 April 2012

.css was not loaded because its MIME type, "text/html", is not "text/css"

I was working on a web application and one of my CSS files was not loading in Firefox (ESR 10.0.2 and 3.6). Using Firebug I could see I was getting this error:

[my css file].css was not loaded because its MIME type, "text/html", is not "text/css"

A Google search will say this is either an issue with labelling the type or a server issue. For me the problem was with the DOCTYPE in my JSP. If I remove the line "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" from the page, Firefox is less strict and the CSS will be loaded, but with a warning from Firebug (which I choose to ignore).

Thursday, 15 March 2012

Remove duplicates from an ArrayList

A way I have found of removing duplicates from a Collection is doing this is to put the list into a Set (which does not allow duplicates). If you really need the it to be a List then you can add the Set back to it:

ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("test");
arrayList.add("test"); // fill the list..
// etc..
Set<String> set = new Set<String>(arrayList); // add to the set
arrayList.clear(); // clear the list
arrayList.addAll(set); // add the set back into the list without the duplicates

You can also use LinkedHashSet which preserves the order of the Collection:

Set<String> set = new LinkedHashSet<String>(arrayList);

Wednesday, 14 March 2012

Ant target shortcuts in NetBeans

To solve some time, Ant targets can be given short cuts in the NetBeans IDE. To do this:

1. Open the build.xml  (e.g. through the Files tab). Within the Navigator tab you will see the file contents (i.e., the targets).
2. RIght click on a target and select Create Shortcut. A wizard will open that lets you configure the shortcut (e.g., toolbar button or a keyboard shortcut).

Monday, 12 March 2012

NetBeans: Unrecognised project; missing plugin?

I recently upgraded my NetBeans to 7.1.1 from 6.8. After installation I could not open my web application projects, receiving the error: Project Name: [unrecognised project; missing plugin?].

To fix this you need to add the EAR project plugin (via Tools>Plugins). The plugin is named EJB and EAR. Once installed, the problem should no longer persist.