Monday 26 November 2012

Unexpected error during Oracle Service Bus synchronization.

I was attempting to export my OSB project as a Jar in Eclipse OSB 11g, but I was getting this error (visible in the Problem tab) which was halting my progress:

Unexpected error during Oracle Service Bus synchronization. Please see the logs for more information.

A solution I found for this was:

  1. Close all projects (including the OSB Configuration Project) in the Eclipse Project Explorer, except for the OSB Project you are trying to export.
  2. Refresh the OSB Project
  3. Reopen the OSB Configuration Project that corresponds to the OSB project




Tuesday 13 November 2012

Eclipse OSB 11g: "This project is not associated with an Oracle Service Bus Configuration"

If you receive the error:

"This project is not associated with an Oracle Service Bus Configuration"

Go to Oracle Service Bus perspective (top right). Now you can drag your project into a OSB Configuration project in the Project Explorer. If there is not an OSB Configuration project available, create one (File > New > Oracle Service Bus Configuration Project).

Wednesday 11 July 2012

The Java EE server classpath is not correctly set up - server home directory is missing.

I received this error when running 'clean dist' for my Java web application from Jenkins but it can also occur in NetBeans:

The Java EE server classpath is not correctly set up - server home directory is missing.
Either open the project in the IDE and assign the server or setup the server classpath manually.
For example like this:
ant -Dj2ee.server.home=<app_server_installation_directory>

The solution I found for this was to add this line to nbproject/project.properties:

j2ee.server.home=http://localhost

After this change the build was successful.

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.

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

Wednesday 22 February 2012

copylibs doesn't support the "rebase" attribute

This can happen when a NetBeans 7.1 project opens a project with an older version of CopyLibs. The solution is;

- Tools->Libraries
- select the "Library location" that you want to fix (not the Global one)
- select the "CopyLibs Task"
- click the "Remove" button on the lower left corner
- click "Ok"

- In the project explorer right click on the "Libraries" virtual folder under
your project
- click "Add Library..."
- click the "Import" button
- select on "CopyLibs Task"
- click the "Import Library" button
- click the "Cancel" button

This can also happen the other way round i.e. when a version of NetBeans less than 7.1 opens a 7.1 project. The solution is;

- Open the file nbproject/build-impl.xml
- remove the rebase attribute and value from the line the error is coming from which is a <copylibs/> tag
 

Tuesday 21 February 2012

Installing .msi files with wine

In a terminal enter;

wine msiexec /i your_file.msi

This will open the file as an exe allowing you to install/run the application.

To reboot wine enter;

wineboot

Linux execution/boot levels

This is the first process that is run by the Linux kernel and is what loads the rest of the system.

There are a number of different execution/boot levels i.e. the mode the computer operates.

The default init is set by a variable called initdefault found in /etc/inittab.

The execution levels are;

0: Halt (i.e. shut down)
1: Single user - reduced set of services, used if others do not boot
2: All services excluding network
3: Full user (no GUI)
4: Not used
5: Full user (with GUI)
6: Reboot

To change the init level enter init [mode] (e.g. init 3). To change the init on startup, change the initdefault value in the inittab file.

To check which level you are on use the who -r command.

For more commands use man init.