Why doesn't groovy use the classpath argument?

A groovy script call using the CLASSPATH prefix, as shown below:

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

but changing it to use classpath arg does not:

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

and gives an error:

 script_from_command_line: 1: unable to resolve class stuff.XMLUtils 

Can someone explain why this is? (The stuff.XMLUtils file is just a few groovy script that I compiled into / path / to / classes)

I did some investigation and used the following groovy script to reset 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 argument -classpath I don’t see an entry in the classloader for the class passed to argpath (in fact, the only directory is the current working directory), for example:

 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 that I set in the variable.

And if I add debugging to the groovy shell executable, the difference in the java call is that the arg -classpath version -classpath not add entries to the java classpath element (which ultimately is the reason that it gives a class error not found) but version CLASSPATH=... adds the path.

Is this a mistake in groovy?

EDIT: A Simple Example of Failure

 - - - - 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 links to stuff , a bad example will work fine.

+6
source share
3 answers

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

I used the default groovy packages from yum in Fedora, however I found a lot of problems (errors starting with groovysh, etc., unable to find the jline package, etc.), and completely switched to using the downloaded versions from codehaus.org and manually specifying GROOVY_HOME and the edit path for calling uploaded.

Now all my examples work as expected.

+4
source

I am in MSYS / Win32 + groovy 2.2 RC1 and have another twist:

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

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

For some reason, none of the above questions will work in my case.

+3
source

This is strange. I just tried to repeat the explained question, but everything seems to work fine (I ran tests with Groovy -Version 1.8.6, 1.7.7 and 1.7.0 on my Ubuntu computer).

So which version are you using and what is your operating system?

In Groovy Bug Tracker, I found the following error: On Windows, the command line option for pathpath (-cp / - classpath) was violated . But this error only affects older versions of Groovy (1.5.2, 1.5.3 and 1.5.4). Perhaps updating Groovy will help fix your problem ...

PS: I usually just comment on this, but unfortunately I don’t have enough points for this :).

0
source

Source: https://habr.com/ru/post/909073/


All Articles