From the book Practices of an Agile Developer: Working in the Real World:
Maintain a log of problems and their solutions. Part of fixing a problem is retaining details of the solution so you can find and apply it later.

I used to do this when I started using Linux, everything that took me more as a few minutes to figure out, I wrote down in a small book. I don't know why I never did the same for the problems I encounter in my day job. By posting my problems & solutions here I can read them from everywhere, and who knows, maybe it is useful for someone else too.

Disclaimer: I didn't find the solution to all of the problems listed here, sometimes a colleague or a friend did.


The neverending pain of working with JAR files

23 November 2006, 09:42

You can’t use the File API with a URI representing a JAR file entry, so you can’t list the contents of a directory that is in a JAR file.

When constructing a path to create a resourcestream from an entry in a JAR file you have to use forward slashes ’/’ as path separator, don’t use the File.separatorChar!

<junit> task doesn't work in Eclipse

25 July 2006, 10:49

The junit.jar isn’t on the classpath of Ant by default. The ant-junit.jar is, but that isn’t enough. Adding it to the classpath in your junit task won’t work either because Ant fails before starting the task. The solution is to add the junit.jar file to the Ant classpath. You can do that by going to the Runtime preferences of Ant in Eclipse and adding the junit.jar (the one that gets delivered with Eclipse) to the Global Entries section.

Using TextMate as the Subversion editor

4 July 2006, 00:01

Make sure TextMate terminal support is enabled by going to TextMate -> Help -> Terminal Usage… There should be a file ‘mate’ in your /usr/local/bin. Put the following line in your .bash_profile file: export SVN_EDITOR=’mate -w’

The -w option tells TextMate to wait until the file is closed, else Subversion will open TextMate and immediately continue, thus trying to commit without a message.

formatting numbers in Ruby

2 July 2006, 03:15

You can use regular C style formatting strings with Kernel#sprintf which will return a String. (I was a bit confused thinking that sprintf printed stuff on the console instead of returning a String). A nice shorthand is the String#% method. (”%04d” % 1 => 0001)

dpi in Swing

25 May 2006, 00:10

Don’t try to use the settings of the OS to calculate how many pixels there are in an inch/centimeter. Graphics2D always assumes 72dpi, so it is easy to calculate how many pixels you need to draw to represent a centimeter/inch. Note that in the Java tutorial there is an example where a ruler is drawn in a JScrollPane a la Photoshop which uses the wrong method of calculating the ppc amount.

Font antialiasing in Java 5

9 May 2006, 02:54

Since JDK 1.5, it is possible to set the system property globally with swing.aatext=true. You can do that with java.exe -Dswing.aatext=true on the command line. Unfortunately the results are not always desirable. Sometimes small fonts look worse anti-aliased.

From: http://mindprod.com/jgloss/antialiasing.html

Drawing in .NET from a different thread as the drawing thread (EDT?)

3 May 2006, 06:39

Stumbled upon this in the following case: a method in a Form calls an asynchronous webservice and provides a callback defined in its own class. The callback creates a new form and displays it. What went wrong is that the new form wasn’t displayed correctly. I immediately though of the EDT in Java, looked for something similar and found it: Control.Invoke

I don’t know exactly what happens but this is what I think, deducting from what I know from Java: the callback method doesn’t get called by the thread responsible for drawing (presuming there is such one, like the EDT in Java, couldn’t find a name for the one in .NET). Making modifications to the GUI from another thread as the drawing thread is unreliable and thus you need a way to invoke your code on the correct thread. This is where the above mentioned method Control.Invoke comes into play, it finds the correct thread and executes a passed delegate on that thread.

Again, this is all based upon my knowledge of Java and may be completely wrong, if you have a better explanation, please tell me so.

Converting an Icon to a Bitmap and back in C#

22 April 2006, 09:41

Icon.ToBitmap()
Icon.FromHandle(bitmap.GetHicon())

Changing the mousecursor in a Swing application

18 April 2006, 20:05

See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Component.html#setCursor)
and http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Cursor.html

Writing an application that resides in the tray in C#

9 April 2006, 05:44

Update: After minimizing all windows for the first time after when developing the supplied example project, I noticed that there was still a small titlebar residing in the bottomleft corner of my screen. Setting FormBorderStyle to None hides it completely. (why everybody is saying to put it to FixedToolWindow instead of None escapes me…)

Use a Windows forms application, and set the following properties on the default form:
FormBorderStyle = FixedToolWindow
WindowState = Minimized
ShowInTaskbar = False

The FixedToolWindow property is necessary to keep the application out of the ALT+TAB list.

Use a NotifyIcon to get an icon in the system tray. The actual icon file needs to be attached to the NotifyIcon instance with this.TrayIcon.Icon = new Icon(GetType(), "newsgator.ico");

Other solutions are supposed to be possible.

Done in .NET 1.1 & VS2003

Link to example project in Subversion

Indexes not used on a VARCHAR column in SQLServer2000

9 April 2006, 05:30

From http://jtds.sourceforge.net/faq.html

sendStringParametersAsUnicode (default – true)
Determines whether string parameters are sent to the SQL Server database in Unicode or in the default character encoding of the database. This seriously affects SQL Server 2000 performance since it does not automatically cast the types (as 7.0 does), meaning that if a index column is Unicode and the string is submitted using the default character encoding (or the other way around) SQLServer will perform an index scan instead of an index seek. For Sybase, determines if strings that cannot be encoded in the server’s charset are sent as unicode strings. There is a performance hit for the encoding logic so set this option to false if unitext or univarchar data types are not in use or if charset is utf-8.

Had this problem when using a VARCHAR instead of a NVARCHAR column. A query with a WHERE COLUMN = N’blahblahb’ became rapidly slower and slower as the table grew in size. We couldn’t figure out why because we already put an index on the column.