Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy : Unable to resolve class

Tags:

jenkins

groovy

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.

like image 681
Glen Avatar asked Jul 13 '17 06:07

Glen


2 Answers

Your problem in classpath and package usage.

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
like image 112
daggett Avatar answered Nov 14 '22 11:11

daggett


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.

like image 45
Andras Avatar answered Nov 14 '22 10:11

Andras