3

A simple Groovy Class in a package com.something

package com.something

class A {
}

Another class in the same package

package com.something

class B {
    def variable=new A() //DOES NOT WORK TILL I EXPLICITLY say "import com.something.A"
}

Why Class B is not able to access class A, even though they both are in the same package?

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Vivek
  • 1,640
  • 1
  • 17
  • 34
  • 2
    How are you compiling? What's your file structure? – tim_yates Jan 02 '14 at 17:47
  • 1
    Both are groovy classes and both reside in the same project and in the same physical folder, right? – akhikhl Jan 02 '14 at 20:17
  • what do you mean by "does not work" - does not compile? does not run? IDE is saying you need to import? Which IDE are you using? – Dror Bereznitsky Jan 02 '14 at 21:07
  • Both are in the same project ofcourse. It does compile but fails while running – Vivek Jan 03 '14 at 03:43
  • @tim_yates class B is used by a groovy script which is bind to IZPack, A Java based installer, lifecycle methods. The groovy script for IZPack lies in a different maven module, which has a dependency on the module which contains these two classes. – Vivek Jan 03 '14 at 03:45
  • What IDE are you using? This should not be this way as long as both are in the same folder and package. – AnthonyMDev Jan 03 '14 at 19:51
  • Intellij IDEA. But it is compiled externally using command line Maven. IDE is just for editing purpose – Vivek Jan 06 '14 at 11:23
  • What version of Groovy are you using? Could you share the relevant bits of your pom? – Keegan Feb 21 '14 at 15:33

1 Answers1

1

Sadly the question does not really have the needed information to answer it completely. But I can tell the following. If you make a directory ./com/something/ and out A.groovy and B.groovy in there and then compile them using the commandline groovyc ./com/something/A.groovy ./com/something/B.groovy, then this must work. So far the directory is not really important, but that changes if you change to groovyc ./com/something/B.groovy, because now groovyc has to "discover" A.groovy and requires the correct directory structure for this.

Now, how the ant, gradle and maven version of groovyc normally work is by supplying a complete list of sources. If this is not done, compilation may fail. But if the root directories for A and B are different and you do not give both to the compiler, then it will surely fail.

I cannot know if that is the reason, so I want this answer more understood as a pointer to what might be wrong. Hope this helps

blackdrag
  • 6,413
  • 2
  • 26
  • 38