Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoClassDefFoundError: Unable to load class groovy.util.OptionAccessor due to missing dependency Lorg/apache/commons/cli/CommandLine #2

Closed
andre-r opened this issue Jun 20, 2017 · 4 comments
Milestone

Comments

@andre-r
Copy link

andre-r commented Jun 20, 2017

When I start a script (./groovyw script.groovy) which is using groovy.util.CliBuilder I get this exception:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during class generation: java.lang.NoClassDefFoundError: Unable to load class groovy.util.OptionAccessor due to missing dependency Lorg/apache/commons/cli/CommandLine;

java.lang.RuntimeException: java.lang.NoClassDefFoundError: Unable to load class groovy.util.OptionAccessor due to missing dependency Lorg/apache/commons/cli/CommandLine;
	at org.codehaus.groovy.control.CompilationUnit.convertUncaughtExceptionToCompilationError(CompilationUnit.java:1089)
	...

It works when I use groovy script.groovy.

script.groovy:

def cli = new CliBuilder(
        usage: 'script.groovy [options]',
        header: 'Options:')
cli.with {
    h longOpt: 'help', 'Show help message'
    _ longOpt: 'test', 'Test option'
}

OptionAccessor opts = cli.parse(args)
if (!opts) {
    System.exit(1)
}
if (opts.h) {
    cli.usage()
    System.exit(0)
}
if (opts.test) {
    println "With test option"
} else {
    println "Without test option"
}

Is it a bug or am I using it wrong?

$ ./gradlew --version

------------------------------------------------------------
Gradle 3.3
------------------------------------------------------------

Build time:   2017-01-03 15:31:04 UTC
Revision:     075893a3d0798c0c1f322899b41ceca82e4e134b

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_131 (Oracle Corporation 25.131-b11)
OS:           Linux 4.4.0-79-generic amd64

@Cervator
Copy link
Member

Thanks for the report! It is most likely that I simply haven't added the provider for the command line stuff to the classpath the script is started with when you use the wrapper. I'm expecting a few of those issues since the way the wrapper is put together is extremely barebones. When you launch Groovy normally (or Gradle) you get a bunch more stuff on the classpath automatically.

I appreciate the script example! Should be an easy fix, will dig around :-)

@Cervator
Copy link
Member

Fixed :-)

Pull the latest or download at https://github.com/MovingBlocks/groovy-wrapper/releases/tag/v1.0.2

Let me know if it works - I copy pasted your example into a script locally and got it working by adding commons-cli from the Gradle dist. Not really sure how much it makes sense to add from there - I doubt there's anything wrong in adding some of the basics like this, but there's probably a line somewhere after which it gets silly.

That's a way off though and I want this to be useful :-) Am actually surprised somebody else found it so relatively quickly, how'd you find it?

For more rare dependencies (and things not shipped with Gradle) it probably makes sense to use @Grab to fetch lib jars from an actual script. As an example I do that over in MovingBlocks/Terasology#2979 which is the first place I'm using the wrapper, to do some Git utility in that case:

@GrabResolver(name = 'jcenter', root = 'http://jcenter.bintray.com/')
@Grab(group='org.ajoberstar', module='grgit', version='1.9.3')
import org.ajoberstar.grgit.Grgit

@Cervator Cervator added this to the v1.0.2 milestone Jun 20, 2017
@andre-r
Copy link
Author

andre-r commented Jun 21, 2017

Thank you, my script works now. I just thought it should work by default since I was using groovy.util-classes. For other classes not shipped with Gradle I already have a @Grab in my script, that works. After using groovyw I also tried @Grab with commons-cli, but with the same error.

I found your groovy-wrapper as I was looking for a way to execute a groovy script on a windows machine where there is no groovy installed (but gradlew existed already in the code base). Actually I found another groovy-wrapper first, but this blends in better. 😁

@Cervator
Copy link
Member

Glad it worked out! Both the fix and the utility. Cool to know there is in fact another Groovy Wrapper out there, I thought it would be weird to have nothing. I do like the sneaky free ride this one gets via the Gradle Wrapper :D

keturn added a commit to keturn/groovy-wrapper that referenced this issue May 17, 2020
It's no longer included in the current gradle distribution.

Breaks support for issue MovingBlocks#2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants