Mark Fisher
Mark Fisher

Reputation: 9886

Why doesn't groovy use classpath argument?

Invoking a groovy script using CLASSPATH prefix as follows works fine:

CLASSPATH=/path/to/classes groovy -e "(new stuff.XMLUtils()).printIt('test string')"

but changing it to use the classpath arg doesn't:

groovy -classpath /path/to/classes -e "(new stuff.XMLUtils()).printIt('test string')"

and gives the error:

script_from_command_line: 1: unable to resolve class stuff.XMLUtils

Can anyone explain why this is? (The stuff.XMLUtils is just some groovy script i've compiled into /path/to/classes )

I've done some investigation, and using the following groovy script to dump the classloader

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

With the -classpath arg, i see no entry in the classloader for the passed in classpath arg, (in fact, the only directory is the current working dir), e.g.:

groovy.lang.GroovyClassLoader$InnerLoader@4911b910
groovy.lang.GroovyClassLoader@18203c31
sun.misc.Launcher$AppClassLoader@35a16869
- file:/usr/share/java/ant.jar
- ... (removed for brevity)
- file:/home/admin/groovy/
sun.misc.Launcher$ExtClassLoader@77cde100
- file:/usr/java/jdk1.6.0_23/jre/lib/ext/sunjce_provider.jar
- ...

Using the CLASSPATH=... version shows that the PWD entry above is replaced by the value i've set in the variable.

And if I add debug to the groovy shell executable, the difference in the java call is that the -classpath arg version adds no entry to java's classpath entry (which is ultimately why it's giving a class not found error), but the CLASSPATH=... version does add the path.

Is this a bug in groovy?

EDIT: simple failing example

- - - - xu.groovy
package stuff
def printIt(string) { println string }
- - - -

groovyc -d classes xu.groovy
groovy -cp classes -e "(new stuff.xu()).printIt('test')"  # fails
CLASSPATH=classes groovy -e "(new stuff.xu()).printIt('test')"  # works

If I remove the package and references to stuff the failing example will work fine.

Upvotes: 8

Views: 16380

Answers (3)

Simon Thum
Simon Thum

Reputation: 594

I'm on MSYS/Win32 + groovy 2.2 RC1 and have another twist:

groovy -cp "./*" script.groovy    // Works!

but

groovy -cp some.jar script.groovy  // ... not

For some reason, although some.jar was in the same directory the above would not work out in my case.

Upvotes: 5

Mark Fisher
Mark Fisher

Reputation: 9886

Answering this myself because I found a solution to the problem.

I was using the default groovy packages from yum in fedora, however found many issues (errors starting groovysh etc, unable to find jline package etc), and have wholly moved over to using downloaded versions from codehaus.org, and manually specifying GROOVY_HOME and editing path to invoke the downloaded one instead.

Now all my examples work as expected.

Upvotes: 6

Stephan Kulla
Stephan Kulla

Reputation: 5067

That's strange. I just tried to repeat your explained issue but everything seems to work fine (I made tests with Groovy-Version 1.8.6, 1.7.7 and 1.7.0 on my Ubuntu Computer).

So which version do you use and what is your operating system?

In the Groovy Bug Tracker I have found the following bug: Command line option for classpath (--cp/--classpath) is broken on Windows. But this bug just affects old versions of Groovy (1.5.2, 1.5.3 and 1.5.4). So maybe an upgrade of Groovy will help to fix your problem...

PS: Normally I just would comment this, but unfortunately I haven't enough points for doing this :).

Upvotes: 0

Related Questions