Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
When I run my Groovy script via the command-line or Jenkins, I am getting an "unable to resolve class" error.
I have the following 2 groovy files within the same folder in C:\Users\myuser\git\productname\mycompany-build\src\main\groovy\com\mycompany\build
Foo.groovy
package com.mycompany.build
class Foo {
Foo() {
public void runBar() {
Bar bar = new Bar();
bar.name = "my name";
System.out.println(bar.name);
static void main(String[] args) {
Foo foo = new Foo();
foo.runBar()
Bar.groovy
package com.mycompany.build
class Bar {
String name;
I run Foo.groovy using the command-line.
I am located within the following directory when I run Groovy:
C:\Users\myuser\git\productname\mycompany-build\src\main\groovy\com\mycompany\build
This is what I enter on the command-line (cmd):
C:/java/tools/groovy-2.4.11/bin/groovy -cp C:/Users/myuser/git/myproject/mycompany-build/src/main/groovy/com/mycompany/build Foo.groovy
I get the following where it is unable to find the class "Bar", but the Bar.groovy file is in the same directory as Foo.groovy, not to mention that I do specify the -cp as well.
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
C:\Users\myuser\git\myproject\mycompany-build\src\main\groovy\com\mycompany\build\Foo.groovy: 9: unable to resolve class Bar
@ line 9, column 9.
Bar bar = new Bar();
C:\Users\myuser\git\myproject\mycompany-build\src\main\groovy\com\mycompany\build\Foo.groovy: 9: unable to resolve class Bar
@ line 9, column 15.
Bar bar = new Bar();
2 errors
Can you please advise on how I can get this running from the command-line?
Once I am able to get this running, I plan to run this within a Jenkins job. I started off attempting to run this within a Jenkins job, but got the same issue which led me to look at running it off the command-line first.
I did attempt this with forward-slashes and back-slashes, but with no difference in behaviour.
–
To get it work
1. if your class declared in a package com.mycompany.build
then groovy/java will look for it in the folder com/mycompany/build
relatve to classpath. so you need exclude package-folders from your classpath:
groovy -cp C:\Users\myuser\git\productname\mycompany-build\src\main\groovy Foo.groovy
2. you can remove package declaration in both classes. in this case groovy/java will look for classes without package-folder prefix in your classpath: C:\Users\myuser\git\productname\mycompany-build\src\main\groovy\com\mycompany\build
and your command should work.
And if your current folder is folder with groovy classes then command could be simpler:
groovy -cp . Foo.groovy
According to the docs, in classpath you can have only .jar, .zip and .class files. The class Bar cannot be resolced because it is a .java file, not a compiled Java class (.class).
The following works for me:
C:/java/tools/groovy-2.4.11/bin/groovyc Bar.groovy
C:/java/tools/groovy-2.4.11/bin/groovy Foo.groovy
my name
Also note that since Bar.class is in the same folder as Foo.groovy, you don't need to specify the classpath.
I think you just need to set the classpath argument to the directory containing the classes i.e. the one with productname
as opposed to myproject
C:/java/tools/groovy-2.4.11/bin/groovy -cp C:\Users\myuser\git\productname\mycompany-build\src\main\groovy\com\mycompany\build Foo.groovy
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.