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.

Friday, 2 March 2012

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

When I ssh into a server I get this message;

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
[long list of characters..].
Please contact your system administrator.


To get rid of this message I opened the known hosts file on my machine;

vi /home/user/.ssh/known_hosts

I then removed the line for the server I am trying to ssh into and saved the file. Next time I tried to ssh into the server I got the option to add it to my list of known hosts which I accepted.

This warning can appear when Linux is reinstalled or UNIX with OpenSSH.

Wednesday, 29 February 2012

The finalize method

Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected.

Your class can provide for its finalization simply by adding a method in your class named finalize(). Your finalize() method must be declared as follows:
protected void finalize () throws throwable

The finalize() method is declared in the java.lang.Object class. Thus when you write a finalize() method for your class you are overriding the one in your superclass.

If your class's superclass has a finalize() method, then your class's finalize() method should probably call the superclass's finalize() method after it has performed any of its clean up duties. This cleans up any resources the object may have unknowingly obtained through methods inherited from the superclass.
protected void finalize() throws Throwable {
    . . .
    // clean up code for this class here
    . . .
    super.finalize();
}

Monday, 27 February 2012

Run test via code

You can run your JUnit and JWebUnit tests from your own code. The class org.junit.runner.JUnitCore provides the method runClasses() which allows you to run one or several tests classes. As a return parameter you receive an object of the type org.junit.runner.Result. This object can be used to retrieve information about the tests.

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

class RunTests {

    public static void main(String args[]) {
        Result result = JUnitCore.runClasses(TestClass.class);
        for (Failure failure : result.getFailures()) {
            System.out.println("FAIL!: " + failure.toString());
        }
    }
}

http://www.vogella.de/articles/JUnit/article.html

Thursday, 23 February 2012

Target "profile-j2ee" does not exist in the project

This is an error which you can get when attempting to Profile a project in NetBeans. A solution I found is;

"It seems that something has changed in project's build script, so the project 
is only partially modified for profiling, leading to the message you've 
provided. Probably the easiest way how to enable profiling again is to undo all 
project modifications and let the Profiler to integrate with the project again.

To do it, perform following modifications:

 - in build.xml, remove <import file="nbproject/profiler-build-impl.xml"/> line
 - in ./nbproject directory, remove profiler-build-impl.xml file
 - in ./nbproject/private, remove <data 
xmlns="
http://www.netbeans.org/ns/profiler/1" version="0.4"/> line
 - (optional) remove ./nbproject/private/profiler directory to remove all 
Profiler settings related to the project

Now after clicking the Profile (Main) Project action again, the Profiler 
notifies you about build script modification and profiling should work again."


From: 
http://netbeans.org/bugzilla/show_bug.cgi?id=73742