Monday, December 17, 2012

groovy: how to print the classpath

Found this really helpful blog post that got me printing out the classpath in one of my Groovy classes. I was debugging why my logback.groovy configuration wasn't getting picked up (because, inded it wasn't on the classpath!)

From the logback FAQ:
Where should the configuration files such as logback.groovy, logback-test.xml or logback.xml be located on the classpath?
Configuration files such as logback.groovy, logback-test.xml or logback.xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.jar;c:/mylibs/" then the logback.xml file should be located directly under "c:/mylibs/", that is as "c:/mylibs/logback.xml". Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.

Here's how to print the classpath, thanks to the original poster:

def printClassPath(classLoader) {
  println "$classLoader"
  classLoader.getURLs().each {url->
     println "- ${url.toString()}"
  }
  if (classLoader.parent) {
     printClassPath(classLoader.parent)
  }
}
printClassPath this.class.classLoader

Thursday, December 13, 2012

how to fix groovy console warnings

Was getting these annoying messages repeatedly in groovysh and groovyConsole:


java.util.prefs.BackingStoreException: java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl cannot be cast to javax.xml.parsers.DocumentBuilderFactory



Solution: delete xml-apis and xercesImpl jars from your ~/.groovy/lib directory:


enkomion [15:20:56] ~/.groovy/lib $ rm xml-apis-1.4.01.jar
enkomion [15:20:59] ~/.groovy/lib $ rm xercesImpl-2.10.0.jar