Finding Java Classes

I’m back on a Java project. Been a while, and I want to capture some of the tricks I’m using.

Right now, I’m just trying to import the project into eclipse.  Seems that the current team members don’t use it.  I’m an IDE kind of guy, at least when it comes to Java.

Building the .classpath file can be tricky.  However, since I know that I have a good build, and that this project it a good participant in the Fedora build process, I have the advantage of knowing that my packages reside in /usr/share/java.  Still, all eclipse gives me is a set of classes that it can’t find.  how to find them?

This project uses CMake.  I could look for all of the Jar files in the CMakeLists.txt files, and I might do that in the future.  However, a trick I’ve developed in the past has come in handy.

 

class2path(){
echo $1 | sed 's!\.!\/!g'
}

JDIR=/usr/share/java

make_alljars(){
for JAR in `find /usr/share/java -name \*.jar -type f ` 
	do for CLASS in `jar -tf $JAR |  grep \.class` 
		do echo $JAR $CLASS  
	done 
done > /tmp/alljars.txt
}

 

First, the make_alljars function creates a map in (value key) order. The value is the Jar file name, and the key is the class name. To fine a Jar file that contains a given class (in this example netscape.ldap.LDAPConnection) , run:

 

grep `class2path  netscape.ldap.LDAPConnection` /tmp/alljars.txt

And the output is

/usr/share/java/ldapjdk.jar netscape/ldap/LDAPConnection$ResponseControls.class
/usr/share/java/ldapjdk.jar netscape/ldap/LDAPConnection.class


This works really well with eclipse, in that the error messages have the name of the class. You can then just highlight the class name, paste it into the command line in place of the class I have above, and when you get the Jar file name, you can highlight to save to the clipboard. From The right click context menu pick Java Build Path and then Add External Archive and then paste the whole path in.

IPAddress for local Virtual Machines

When running Fedora as a KVM/Qemu  host for  virtual machines, you have the issue that you don’t know the IP Address for a virtual machine once you create it.  IP addresses that are assigned via

The MAC Address is in the config file saved in

/etc/libvirt/qemu/$VMNAME.xml

Once you start the virtual machine, you can fetch the IP Address from the DHCP lease file in:

/var/lib/dnsmasq/dnsmasq.leases

To correlate the two:

 

#!/bin/bash

VMNAME=$1

MAC=`cat /etc/libvirt/qemu/$VMNAME.xml |   xml2 | awk 'BEGIN{FS="="} /mac..address/ {print $2}'`

IP=`grep $MAC /var/lib/dnsmasq/dnsmasq.leases | cut -d' ' -f3`

#$VMNAME has MAC $MAC and IPAddress $IP
echo $IP

 

This must be called as root or via sudo.

UPDATE:

Chris Lalancette notes that the cannonical version of the MAC address can be found using

virsh -c qemu:///system dumpxml $VMNAME

Removing empy comment blocks

Eclipse can automate a lot of stuff for you. One thig is did for me was automating the serialVersionId generation for all the serializable classes in my tree.
They look like this:

    /**
     *
     */
     private static final long serialVersionUID = -9031744976450947933L;

However, it put an empty block comment in on top of them, something I didn’t notice until I had mixed in this commit with another. So, I want to remove those empty comment blocks.

#!/bin/bash

for JAVAFILE in `find . -name \*.java`
do
     sed -n '1h;1!H;${;g;s! */\*\*\n *\* *\n *\*/ *\n!!g;p;}' \
         < $JAVAFILE > $JAVAFILE.new
     mv $JAVAFILE.new $JAVAFILE
done

Thanks to this article for how to do the multiline search and replace.
http://austinmatzko.com/2008/04/26/sed-multi-line-search-and-replace/

Snapshot VMs

This past week at the  Red Hat summit I got the chance to demonstrate Enterprise IPA, the Red Hat version of FreeIPA, at the Red Hat booth.  One of the aspects of IPA we want to showcase is registering client systems.  That means that I wanted to be able to get a client system in the pre-installed state pretty very quickly.  My approach was to use Qemu/KVM virtual machines.  I had one VM image that I did not touch, and all the rest of the virtual machines will be snapshots that overlay that image.

Continue reading

Popup notifications

I am easily distracted. If a build takes more than say, three seconds, I usually will flip to doing something else. This means that I often miss when a build is completed, and end up losing a few minutes here, a few minute there.

Well no longer. I use Zenity! What is this you ask? I didn’t know either until today. Zenity is a command line tool for making a popup window appear.

Now My build scripts look like this:

mvn -o -Pdev install
zenity –info –text “Build is completed”

This kicks off the build, and, when it is done, I get a lovely popup window telling me: the build has completed.

As the Corollary to Murphy’s law states: If its stupid, but it works, it ain’t stupid.

Why zenity? I mean, there are at least a dozen different ways to popup a window. Well, in keeping with that Cardinal programmer virtue of laziness, it is because zenity is in the Fedora11 repo, and I am running Fedora 11. yum install is my friend.

Yes, I realize that if I were cooler, I would make my script tell me success versus failure, and pop up the appropriate window for that. I’m not that cool.

OK, I wanto to be cool. Here’s the new version:

mvn -o -Pdev install && zenity –info –text “Build is completed” || zenity –warning –text “Build Failed”

This pops up a warning message box on mvn returning non-zero for failure. Note the use of the && and the ||. The evaluation of this is kind of cool: The && (logical and) has short circuit semantics, so the second portion only gets evaluated if the first part evaluates to true. However, the || (logical or) only gets evaluated if everything before it fails.