Oct 11 2011

Keep Code Statements Simple

I don’t count my progress by the line of codes but at the same time I don’t take pride by over engineering a solution. Writing code is like writing for a publication, you have to know at what reading level you are writing for. That said, the one type of code statement that gets under my skin is what I call the run-on code statement. A Run-on code statement is one that has multiple method calls in one statement. Here is a made up example of a run-on code statement.

DataManager.getInstance().refreshData(obj.getAsInteger().toString());

In the above run-on code statement there are four method calls. I’ve seen worse. The reason whey run-on code statements are a pet peeve or mine is that if anyone method call fails because of a NullPointerException or some other error it’s difficult to quickly know what segment of the code statement failed. This is also annoying to debug if you want to step into one method out of the four.


Sep 29 2011

Find the Current Working Directory in Java

There are times when you don’t have full control of the location where your Java application runs from. This could happen because the application is installed in a location other than the one recommended by the installer, or because it ran from the IDE, or some other reason. For whatever reason, if you need to find the current working directory where your Java application runs there are two different approaches. The first approach is to use the File class and the current directory symbol to find the current directory. Remember that the single period “.” represents the current directory and two periods “..” represents the parent directory.

   String currentPath = new File(".").getCanonicalPath();

Unfortunately, using the File class throws an IOException. There is another approach that does not throw an exception and returns the same absolute path to the current directory where the Java application is running from.

   String currentPath = System.getProperty("user.dir");

Sep 9 2011

Remove Multiple Null Values From A List in Java

I’ve had situations where I’ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries. Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple occurring value from a lists in java. There are a few ways you can approach this problem. You you can remove each occurrence of a list element one at a time. The example below removes any null element in the list one at a time.

while(ids.contains(null)) {
	ids.remove(null);
}

There is another approach you can use to remove all instances of a given value from a list in one go. Instead of the remove method you can use the removeAll as in the following code sample.

ids.removeAll(Collections.singleton(null));

Of course, instead of null values you can remove all instances of any given value from a list based on your business needs.


Sep 8 2011

Launch Default Web Browser in Java

For the longest time I’ve used the BrowserLauncher library to open the default web browser to a specified web page from Java. BrowserLauncher is simple to use, just import edu.stanford.ejalbert.BrowserLauncher and call openURL method with the desired website URL.

Since Java 1.6, the JDK has introduced the java.awt.Desktop class to do the same so you don’t need an additional third party jar. The Desktop class has the ability to launch the desktop’s default email client and default web browser given a URI. Here is how you can launch the desktop’s default web browser in Java.

// Launch your default email client with ...
URI email = new URI("mailto:myemail@mydomain.com");
Desktop.getDesktop().mail(email);

// Launch your default web browser with ...
URI url = new URI("http://www.mydomain.com");
Desktop.getDesktop().browse(url);

Jun 27 2011

Where To Download Previous Versions of Java

Earlier this week I received the following Skype message from a co-worker.

[10:10:15 AM] i can not find an where to download official Java 6 update 23
[10:10:21 AM] mostly its from third party sites

I can’t even begin to state how many things I find wrong from the above message. Normally I would just reply with a link to Let Me Google That For You, such as the following search for Java 6 Update 23 download. I decided against being a wise guy, and found the Java archive site because it seen this question come up before for other software packages. Not all, but most software vendors such as Oracle, MySQL, Perforce, and others make available previous versions of their tools or software. It’s usually in a small link at the bottom of the main download the reads software archive, older version, previous releases, or something to that effect.

In the case of Java, Oracle has a Java Technology Products Download page where you can find the older versions of the JDK, JRE, and other Java framework and toolkits.


Apr 29 2011

Can You Spot the Infinite Loop?

Can you spot the infinite loop in the following snippet of code?

int i = 0;
{
    i++;
}while(i < 5);

The above code caused a critical bug in a application I was working on. At first sight, the code looks okay, especially since it compiles. It’s a do-while loop with a condition that seems that it would break when the variable i is equals or greater than 5. The variable i is set and incremented correctly but unfortunately this causes an infinite do nothing loop. Did you spot the problem? An important keyword is missing from the do-while loop, the do. Let me add comments to explain each statement as the compiler sees it…

// Initialize the variable i
int i = 0;

// Create a block and increment i by one
{
    i++;
}

// Infinite loop with an empty code block
while(i < 5);

The problem is that the while loop can have zero or one code block of statements (where a single statement can be a code block). If the state of what you are testing in the condition does not change because of the test, or because of the statement you are looping over, then you have an infinite loop. In the above code, because of a badly written do-while loop, this loop does not have statements that update the variables in the condition and so this while loop never breaks out.

The correct code would be the following…

int i = 0;
do {
    i++;
}while(i < 5);