|
|
Java classes in Grails app won't compile
I have a controller that uses a java class in /src/java. The controller, compiled via STS, does not see the java class. Both the controller and java class are in the same package. The error message is:
Groovy:unable to resolve class myPackageName.CalendarModelEventController.groovy /RSN/grails-app/controllers/myPackageNameline 4Java Problem
The application compiles fine as a standalone Grails app; there is only a problem when I import it into STS.
Hi,
Another user just reported something similar in another thread but couldn't recreate it. Is there any way you can share the project containing this problem with me? I can't recreate it either.
I presume the project is in STS and has had the groovy nature properly added - ie. this line in the projects .project file:
lt;naturegt;org.eclipse.jdt.groovy.core.groovyNature lt;/naturegt;
Also, I presume all the source folders for the project, whether they contain groovy code or java code, are properly defined as source folders in the project?
thanks
Andy Clement
Groovy Eclipse
Yes the project is in STS and does have the Groovy nature and has been configured as Grails project. Everything works as expected except for the visibility/scope problem.
I have an event controller that makes use of some calendaring features in that are in java (located in /src/java/myPackage). Both the groovy controller and java class are in the same package. It goes something like this:Code:
class EventController {
def getCalendar {
CalendarModel calendar = new CalendarModel(month, year)
return calendar
}
}
Code:
public class CalendarModel {
public CalendarModel(int month, int year) {
//calendar code here
}
}
I get a compile error at the line:
CalendarModel calendar = new CalendarModel(month, year)
The message is:
Groovy:unable to resolve class CalendarModel
Unfortunately in my attempts to recreate this problem, it is just working for me. New grails project (1.2m3), created controller class:Code:
package com
class SongController {
def getCalendar() {
CalendarModel calendar = new CalendarModel(month, year)
return calendar
}
}
(had to add '()' to the end of the getCalendar snippet you included)
Finally defined the calendarmodel class:Code:
package com;
public class CalendarModel {
public CalendarModel(int month, int year) {
//calendar code here
}
}
On repeated clean builds or incremental builds it just works fine. CalendarModel is in src/java/com whilst SongController is in grails-app/controllers/com
Anything obviously different between my setup and yours? I have package statements and you didn't show them - I presume you had them in your source?
Does it happen on all builds for you, both incremental and clean?
Are you on mac, windows or linux?
Does the workspace path have a space in it?
cheers,
Andy
Thanks, Andy for taking the time to look into this. No spaces, Mac, always clean. I created a new, stripped down application that just had the event functionality so I could send it to you. Surprisingly (and frustratingly), it worked fine--even though it was exactly the same code.
Getting desperate, I noticed that the STS gui rendered the package for src/java structure differently in the stripped down app. My full package name is edu.washington.cev.rsn. In the full featured app, the src/java package structure in the Project Explorer tab was represented by a box and a folder--the rsn directory was shown as a folder. In the stripped down app the package name was just a box--no folder icon.
Thinking there might be some connection between the fact that the compiler was complaining about not being able to see the CalendarModel class and the difference in package representation, I moved the java classes to edu.washington.cev and changed the import declaration in Event.groovy to match (import edu.washington.cev.CalendarModel). And then it compiled.
I wish I could say felt better about having it compile. For what its worth I've attached a screenshot of the gui.
That is a very cool bit of detective work you did and may help me finally solve this horrible problem.
I can cause your scenario to happen if I do a particular thing. A 'package' can be turned into a 'folder' if excluded from the build (and it would cause the missing class issue). Can you possibly check your .classpath file?? When I exclude a folder in mine, I get a special entry in my .classpath file. On the second line below you will see an exclude - that causes bar to appear as a simple folder in the com.foo package rather than as a real package itself. Do you have any excluding lines like that in yours?Code:
lt;classpathgt;
lt;classpathentry excluding=quot;com/foo/bar/quot; kind=quot;srcquot; path=quot;src/javaquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;src/groovyquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;grails-app/confquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;grails-app/controllersquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;grails-app/domainquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;grails-app/servicesquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;grails-app/taglibquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;test/integrationquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;test/unitquot;/gt;
lt;classpathentry kind=quot;conquot; path=quot;org.eclipse.jdt.launching.JRE_CONTAINERquot;/gt;
lt;classpathentry kind=quot;conquot; path=quot;com..sts.grails.core.CLASSPATH_CONTAINERquot;/gt;
lt;classpathentry kind=quot;srcquot; path=quot;tomcat-1.2-M3-src-groovyquot;gt;
lt;attributesgt;
lt;attribute name=quot;com..sts.grails.core.SOURCE_FOLDERquot; value=quot;truequot;/gt;
lt;/attributesgt;
lt;/classpathentrygt;
lt;classpathentry kind=quot;outputquot; path=quot;web-app/WEB-INF/classesquot;/gt;
lt;/classpathgt;
After confirming whether you have that, you could try right clicking the 'rsn' folder - do you have the menu option BuildPathgt;Include ?
Either:
- your .classpath file is not saying it is excluded and we have a realllllllllly nasty problem
- or, for some reason on import the project .classpath is populated incorrectly.
i really hope it is the latter.
cheers,
Andy
Well you are right--the src/java directory was excluded:
lt;classpathentry excluding=quot;edu/washington/cev/rsn/quot; kind=quot;srcquot; path=quot;src/javaquot;/gt;
I'm pretty sure I didn't exclude it when I imported the project. It's great to have an explanation though!
-Shawn
Thought I'd follow up here since I did a new post on this issue (showthread.php?t=86338) but saw this one after the fact (sorry about that!).
I have this exact situation happening but I don't have any excluding entries in .classpath. I also have one file in src/java that *does* get compiled (a servlet filter that has a reference in a plugin's doWithWebDescriptor) but it seems to ignore anything else in src/java, even if it's in the same package as the servlet filter that does get compiled. And everything does get included if I do grails war.
Anyone have any other ideas? I can of course build the war and deploy it but it definitely slows down the development process.
Thanks. |
|