25 June 2009

Eclipse 3.5 Galileo - a first impression


I installed Eclipse 3.5 (or Galileo as it is called) today.

My first impression is that it is similar to 3.4. One thing that i noticed is that cancellation of building a Java project is working better in 3.5. The Java editor is updated and writing code is a bit different. For instance I got a bit annoyed by how parenthesis are inserted. But I guess that I'll quickly get used to it.

The "editor" for Team Synchronizing compare view is updated so it now is more like a Java editor. I'm note sure I will edit code in a compare view just now. I still miss the Ctrl + Shift + T shortcut in Team Synchronizing.

24 June 2009

Key bindings is sometimes to prefer over KeyListener

In Java (Swing) there are KeyListener:s and key bindins. KeyListerner is the classical way of catching events from the keyboard. But inn some situations the key bindings feature is preferred:

  • If you want to catch (only) the CTRL key

  • If you want to catch keys regardles of where the user has focus


For example if you are developing a tetris game this code can be used to get the Left arrow key:
        getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
getActionMap().put("left", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
engine.moveBrickLeft();
}
});


To catch the control key use KeyStroke.getKeyStroke("ctrl pressed CONTROL") and KeyStroke.getKeyStroke("released CONTROL")

More information at http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html

04 June 2009

Hint: Set a good comment on your ssh key

Using a ssh key to login into a ssh account is really good. You don't have to know the passwords of the accounts you are logging into, only the password on the ssh key (if any). If you use a password on the key the password can be cached (for example using Pageant).

When creating a ssh key I really recommend that you set a good comment. For example using puttygen enter a "Key comment":


This should be included in the key:
  • The identity of you (for example your e-mail)

  • The computer you used to create it on (for example your Windows computer name)

  • The date you created the key

This way it will be so much easier when someone should clean up the keys from .ssh/authorized_keys. The identity is good if there are many users for the same ssh account (common in some corporations). The computer name and date is useful when you know that you have lost the key (for example because of hard drive crash), of when a security exploit has been published and the key is considered unsafe.

03 June 2009

Clean your laptop from dust periodically

A colleague of mine had some problems with very high temperatures in a laptop. The temperatures was about 71 degrees Celsius at idle and 91 degrees Celsius a full load. (It was a HP laptop with some kind of dual core). I opened the laptop up and took out the fan. In the space between the outlet of the fan and the heat pipe there was a rather thick layer of dust. I cleaned it and now the temperatures where about 5 degrees lower.

I my colleague's case there is probably something else wrong with the fan (or computer), but it is clear that a little bit of dust defensively can have an effect on the temperature (and sound level).

02 June 2009

Breakpoint problem with Eclipse 3.4 and JDK 1.6 update 14

I had problems that some break points did not take in my Eclipse 3.4.1 when I updated from JDK 1.6 update 12 to JDK 1.6. update 14. Well some breakpoints took, but none in the AWT thread. It could be a coincidence, I don't know.

I updated to Eclipse 3.4.2 but that did not solve the problem.

I had to downgrade to JDK 1.6 update 13 to solve the problem. It was enough to choose another JDK for my specific project. Eclipse itself could run update 14.



I searched the Internet but could not find any official bug either in Java or Eclipse. Anybody out there got more information?

Update: the problem is now fixed: http://blog.lesc.se/2009/09/jdk-breakpoint-problem-fixed-in-update.html

01 June 2009

Java VisualVM, a better JConsole

A new tool is available for Java developers: VisualVM. JDK 1.6 update 7 and later comes bundled with this new tool. The executable (jvisualvm) can be found in bin or the tools directory.

The first thing I recommend doing is to install the MBean plugin. With this plugin VisualVM gets the mbean feature found in JConsole. In fact I never uses JConsole anymore since VisualVM has all the features, and more.

To install the plugin select Tools -> Plugins -> Available Plugins. Check the VisualVM-MBeans and press install:


Now attach to a process by clicking on it in the list. If the process is on another computer use the Remote-node.

This is how the MBeans tab looks like:


This is how the monitor tab looks like:

Btrace – a debugging tool with bugs

I tried Btrace (http://btrace.dev.java.net/) it is a debugging tool for Java. I have often been in the situation that a bug has been found in a production system. The production system must run even as I try to find the cause of the bug. That means the (java) program cannot be restarted to add some remote debugging ports or some other start-up options. This is where Btrace comes in. With Btrace you can write small script-like java code fragments so the program can be traced. Btrace accomplishes this by dynamically instrumenting the code.

Let’s take an example. Let’s say your production system tries to find numbers in the Fibonacci sequence:
import java.util.ArrayList;
import java.util.List;

public class FibonacciService {
private List<Integer> list = new ArrayList<Integer>();

public FibonacciService() {
//First two numbers in Fibonacci sequence.
list.add(0);
list.add(1);

new Thread(new Runnable() {
public void run() {
while (true) {
calculateNext();

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}, "calc-thread").start();
}

private void calculateNext() {
int oldOld = list.get(list.size() - 2);
int old = list.get(list.size() - 1);

int newNumber = nextFibonacci(oldOld, old);
list.add(newNumber);
System.out.println(newNumber);
}

private static int nextFibonacci(int oldOld, int old) {
return oldOld + old;
}

public static void main(String args[]) {
new FibonacciService();
}
}
This Btrace code fragment will print out a trace string to the Btrace console every time the method calculateNext is entered:
  @OnMethod(
clazz="se.lesc.btrace_example1.FibonacciService",
method="calculateNext"
)
public static void onStartCalc() {
println("Starting to calculate new number...");
}
Btrace code fragments can be attached to running java processes in two ways. Either use the command line “btrace” command or use a plugin to Suns Java VisualVM. Java VisualVM is like a Jconsole on steroids. It s is included in the JDK bundle since 1.6 update 7 (or something like that). You will find it in the JDK installation directory (either in tools, or in bin). Follow these instructions to add Btrace capabilities to VisualVM: http://btrace.dev.java.net/visualvm_uc.html.

When installed, right click on the process and select “Trace application…”.


Then paste (or write) the code fragment and press Start. When pressing Start it takes a few seconds for the code to be instrumented. The output is shown in the console below the code fragment.


A little bit more advanced fragment that will print the method arguments when the method nextFibonacci is called:
  @OnMethod(
clazz="se.lesc.btrace_example1.FibonacciService",
method="nextFibonacci"
)
public static void onNextFibonacci(int oldOld, int old) {
print("Input values are ");
print(str(oldOld));
print(" and " );
print(str(old));
println();
}
A fragment that prints out the return value when the nextFibonacci method is returned:
  @OnMethod(
clazz="se.lesc.btrace_example1.FibonacciService",
method="nextFibonacci",
location=@Location(Kind.RETURN)
)
public static void onNextFibonacci(int returnValue) {
print("Result is: ");
print(str(returnValue));
println();
}
There are some restrictions on how advanced the fragments can be. Because the code must never get stuck loops are not allowed. Creating objects arbitrary is not allowed nor calling any methods (except from the BTraceUtils class).

Everything is not good about Btrace:
  • Slow web page. Perhaps not Btrace fault. The entire *.dev.java.net servers are so slow that it is annoying.

  • There is always a risk when instrumenting the code. Out of about 30 runs I had 3 unexpected crashes of the JVM that I can remember. It is with other words somewhat of a risk to run Btrace in a production environment.

  • Some features don’t work. For example I tried to use a regular expression to match a method but every time I got java.lang.VerifyError (Edit: I've now submitted a bug report for it: https://btrace.dev.java.net/issues/show_bug.cgi?id=41). Another example is that I tried to get a reference to the current object in side a method, but it did not work.

  • The documentation is need improvment. For example Btrace uses some kind of signature matching to find methods with arguments. How this matching is done is not documented. Such a central feature is really important to have documented.

  • The project does not look to be actively developed. The last implemented ticket was submitted the 22th of February in 2009. Even critical and easy to fix problems is not fixed. For example in my opinion the bug about that Btrace cannot be run on Windows when there exist a space in the path should really be fixed. Edit: the project apparently has active development, based on the comments on this blog entry. :)