1. Java

Java is a powerful programming language that lets you write programs that run virtually anywhere!

2. Profilers

I've tried both YourKit and JProfiler. Both were sufficient for my needs and integrate well into IntelliJ.

For a free option, try the jvisualvm that comes with the JDK.




Address already in use: connect

MESSAGE: Address already in use: connect

STACKTRACE: Address already in use: connect
	at com.mysql.jdbc.StandardSocketFactory.connect(
	at com.mysql.jdbc.MysqlIO.<init>(
	at com.mysql.jdbc.Connection.createNewIO(
	at com.mysql.jdbc.Connection.<init>(
	at com.mysql.jdbc.NonRegisteringDriver.connect(
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)

It isn't saying that its trying to listen on a local socket, but can't create a socket. Check that an excessive amount of TCP sockets aren't already open using netstat.

5. System Properties

The JVM takes arguments on the command line -Dname=value

e.g. To specify the default timezone, java -Duser.timezone=GMT

To specify an endorsed directory, where jars will take precendence over the default JRE classes, use -Djava.endorsed.dirs

6. JSR

JSR 310: Date time calendar API

We're using Joda Time as a replacement date time API in our code.

7. Patterns and Regular Expressions

Non-Greedy matching

Add the '?' character to the expression to indicate a non-greedy search.

String greedy = "fish<br>cakes<i>penguin".replaceAll("<.*>","");
String nonGreedy = "fish<br>cakes<i>penguin".replaceAll("<.*?>","");
System.out.println("greedy: " + greedy);
System.out.println("nonGreedy: " + nonGreedy);

The above will output

greedy: fishpenguin
nonGreedy: fishcakespenguin

Group matching and back references

To use back references (i.e. referring to a portion of the pattern that was matched), create your pattern, and also add round brackets to surround the groups you are interested in.

An as example, consider the following program snippet:

Pattern compile = Pattern.compile("Your balance is (.*) dollars");
String message = "Your balance is 123 dollars";
Matcher matcher = compile.matcher(message);
if (matcher.matches()) {
    // matched
    String wholePattern =;
    String group1 =;
    System.out.println("wholePattern: " + wholePattern);
    System.out.println("group1: " + group1);

and output:

wholePattern: Your balance is 123 dollars
group1: 123

Note the round brackets that where used in the pattern. This is a group and that can be back referenced.

A matcher is created, and the 'matches' method is called to test whether a match was found.

If a match was found, you can call the 'group' method with '0' to see the whole match, and if you used round brackets or groups, you can refer to specific groups that were matched starting from '1'.

Non-capturing group

Sometimes you want to use a group (i.e. round brackets) to denote logical OR, but do not want to capture the group. e.g. If you want to look for the word apple or banana, you can have a pattern such as:

String pattern = ".*(apples|bananas).*";

This will work when you match, and if you refer, it will return "apples" or "bananas". If you are not interested in the group, you can change it to a non-capturing group by adding a "?:" to the group. e.g.

String pattern = ".*(?:apples|bananas).*";

This way will not exist

8. Monitoring

Setup so you can connect to your instance via jconsole.

For thread dumps, you can use jstack $pid which will display a thread dump for you.

9. Javadoc


Specify the class, and optionally the linkText.

{@link com.magicmonster.example.Class linkText}

10. java.lang.IncompatibleClassChangeError: Implementing class

You probably have duplicate classes from different jars. Check your deployment does not include different versions of the same jar.