Thu 11 Mar 2010

Strip class files from a Java jar

I occasionally hit an issue whereby a Clojure library is built with an earlier version of Clojure, and its included class files cause an error such as

Caused by: java.lang.NoSuchMethodError: clojure.lang.RestFn.<init>(I)V (compojure.clj:9)

The easiest immediate fix is to strip class file from the jar. Awful hacky shell scripting to the rescue! This did the trick. It ain't pretty, but hey.

mkdir $1-tmp
mv $1 $1-tmp
cd $1-tmp
jar xf $1
find . -name '*.class' -exec rm {} ;
jar cf $1 *
mv $1 ..
cd ..
rm -r $1-tmp

Use like this: unclassify foo.jar.

Posted at 2010-03-11 13:27:00 by Richard NewmanLink to Strip class files …