2/27/2007

Don't copy jar files in auto-loaded directories

Jar files in some directories are automatically loaded into JVM in some applications. Some examples of these magic directories are, common/lib and shared/lib in Tomcat, $JAVA_HOME/jre/lib/endorsed, $JAVA_HOME/jre/lib/ext, etc. This can ease the task of classpath management, to some extent. But we should also be very careful while doing anything in these directories. Specifically, do not put any unused *.jar files in these directories, since they will also be loaded by java runtime and may cause conflict.

Sometimes I need to try a different version of a jar. I rename the old jar to foo-02272007.jar and copy the new jar over. In an auto-loaded directory, this may cause nasty random errors due to conflict between 2 versions of the same jar. I should really back up the old jar as foo.jar.20202007 without a .jar extension to exclude it from auto-loading.

2/10/2007

Comment out xml file fragments in java IDE

It's easy to comment out and uncomment lines of Java code in any Java IDE, using shortcut keys like Ctrl-/ (in Eclipse), or Ctrl-Shift-T (in NetBeans). But I haven't found such features for XML commenting. I have to always manually add <!-- and --> even when editing those XML files inside Eclipse or NetBeans.

Why is it missing in major Java IDEs? Nowadays almost all Java projects have some XML files, like ant build files, deployment descriptors, etc. And both NetBeans and Eclipse have nice XML editing features, including syntax highlighting, element and attribute auto-completion, and element tree view. All these seem to suggest Java IDEs are serious in supporting XML development. One would think commenting/uncommenting is one of the basic requirements. Why can't I select some lines, press Ctrl-/ or Ctrl-Shift-T to comment out them?

Maybe one reason is that XML doesn't allow nested comments. So if my selection already contains XML comments, I can't add an outer comment. But even in this case, the IDE can either issue a waring, or add 2 smaller comments, one before the existing comment and the other after.

2/06/2007

Override equals and hashCode methods (part 2)

Eclipse can generate equals and hashCode methods for any class that has state fields (see post). What if you are writing code outside Eclipse, e.g., using a plain text editor? I usually copy java.lang.String.equals(Object) and modify it:

C:\jdk5> jar xvf src.zip java/lang/String.java
inflated: java/lang/String.java

C:\jdk5> gvim java/lang/String.java
It basically looks like this:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
xxx anotherString = (xxx)anObject;
//compare the states of two objects
}
return false;
}
I then save this template in C:\tmp\equals.txt. Next time I need it inside vim, I just read it into vim with :r /equals.txt.