Groovy Console for Grails application "unable to resolve class"

Answered

I'm a total noob with Grails, and I just bought IDEA to try to help me get to grips. I have a simple Grails app with one domain class: Program. When I open the Groovy console, I can start typing "Program", and it will autocomplete. I can type a period, and it shows me suggestions for all the class methods. If I tab-complete .findAll(), it automatically inserts an import statement for the class definition. (I've created one Program in the database by hand.) Yet, when I run the resulting code:

import cdt_dashboard.Program
def ps = Program.findAll()

I get this in the output. (Mind that IDEA inserted the import statement itself!)

/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -Dtools.jar=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar -Dgroovy.home=/Users/david/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.4.14/9023f1b5d771f272f45cfad3b9865482cff33582 "-Dgroovy.starter.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/groovy-starter.conf" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper /private/var/folders/y3/38jcl5nj7zjglhn8q6g0wtrc0000gn/T/idea_classpath org.codehaus.groovy.tools.GroovyStarter --conf "/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/groovy-starter.conf" --main groovy.ui.GroovyMain -p "/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/console.groovy"
> import cdt_dashboard.Program
>
> def ps = Program.findAll()
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
ideaGroovyConsole.groovy: 1: unable to resolve class cdt_dashboard.Program
@ line 1, column 1.
import cdt_dashboard.Program
^

1 error

at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:981)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:626)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:323)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:293)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:677)
at groovy.lang.GroovyShell.run(GroovyShell.java:506)
at groovy.lang.GroovyShell.run(GroovyShell.java:486)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at console.run(console.groovy:11)
at groovy.ui.GroovyMain.processReader(GroovyMain.java:576)
at groovy.ui.GroovyMain.processFiles(GroovyMain.java:485)
at groovy.ui.GroovyMain.run(GroovyMain.java:327)
at groovy.ui.GroovyMain.process(GroovyMain.java:315)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:134)
at groovy.ui.GroovyMain.main(GroovyMain.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:116)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67)

 

0
14 comments

hat module is selected in the Groovy console bar:

Make sure you have selected the module there which has the cdt_dashboard.Program class and you have the built the project (Build | Rebuild Project action).

If issue remains, please attach content of the file which comes right after the com.intellij.rt.execution.CommandLineWrapper argument in the very first line when you launch the console. In your example above it is /private/var/folders/y3/38jcl5nj7zjglhn8q6g0wtrc0000gn/T/idea_classpath file.

0

Aha! Now we're getting somewhere. That file doesn't exist on my machine. My directory is "/private/var/folders/y3/38jcl5nj7zjglhn8q6g0wtrc0000gn/T", but there's no "idea_classpath" file in it.

I also have to do this work on a Windows 8 computer. It doesn't work there either, and for the same reason. I don't have a "C:\Users\xx934\AppData\Local\Temp\idea_classpath" file on that platform.

I've built and rebuilt the project on both, but it doesn't generate the missing file.

 

0

It is possible that this file is created temporarily an gets removed after the process has exited. What module do you have selected in Groovy console - does it have the class in it's source directory? What IDE version do you use?

0

The module with “_main” comes up as the default, but I’ve tried them all.

As of this morning, I got a message about a new version. I updated both machines to the latest version and tried again, and got the same result.

0

Does running Grails application via Grails Run/Debug Configuration work? Please try to re-creating a project by re-importing it as Gradle project.

If issue remains can you provide a sample project to check?

0

Imported as a generic Gradle project. Again, the console recognizes the class structure. Running it gives me what's below.

I can zip this up and send it to you if you want.

/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/bin/java -Dtools.jar=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar -Dgroovy.home=/Users/david/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.4.14/9023f1b5d771f272f45cfad3b9865482cff33582 "-Dgroovy.starter.conf=/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/groovy-starter.conf" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper /private/var/folders/y3/38jcl5nj7zjglhn8q6g0wtrc0000gn/T/idea_classpath org.codehaus.groovy.tools.GroovyStarter --conf "/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/groovy-starter.conf" --main groovy.ui.GroovyMain -p "/Applications/IntelliJ IDEA.app/Contents/plugins/Groovy/lib/console.groovy"
> import cdt_dashboard.Program
>
> def ps = Program.findAll()
java.lang.IllegalStateException: Either class [cdt_dashboard.Program] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
at org.grails.datastore.gorm.GormEnhancer.stateException(GormEnhancer.groovy:469)
at org.grails.datastore.gorm.GormEnhancer.findStaticApi(GormEnhancer.groovy:300)
at org.grails.datastore.gorm.GormEnhancer.findStaticApi(GormEnhancer.groovy:296)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormStaticApi(GormEntity.groovy:1352)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at cdt_dashboard.Program.currentGormStaticApi(Program.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.findAll(GormEntity.groovy:673)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$findAll$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at cdt_dashboard.Program.findAll(Program.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:98)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.invoke(StaticMetaMethodSite.java:46)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.call(StaticMetaMethodSite.java:91)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.findAll(GormEntity.groovy:672)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$findAll.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:128)
at cdt_dashboard.Program.findAll(Program.groovy)
at cdt_dashboard.Program$findAll.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
at ideaGroovyConsole.run(ideaGroovyConsole.groovy:3)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
at groovy.lang.GroovyShell.run(GroovyShell.java:507)
at groovy.lang.GroovyShell.run(GroovyShell.java:486)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)
at console.run(console.groovy:11)
at groovy.ui.GroovyMain.processReader(GroovyMain.java:576)
at groovy.ui.GroovyMain.processFiles(GroovyMain.java:485)
at groovy.ui.GroovyMain.run(GroovyMain.java:327)
at groovy.ui.GroovyMain.process(GroovyMain.java:315)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:134)
at groovy.ui.GroovyMain.main(GroovyMain.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:116)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:67)

 

 

 

0

Relevant application.yml section. The database is correctly configured. I can get into it with `psql -Ucdt2 cdt2_dev` (and no password). I've also `generate-all Program` and I can list the (single) Program in the index page.

hibernate:
cache:
queries: false
use_second_level_cache: false
use_query_cache: false
dataSource:
pooled: true
jmxExport: true
driverClassName: org.postgresql.Driver
username: cdt2
password: ''

environments:
development:
dataSource:
dbCreate: update
url: jdbc:postgresql://localhost:5432/cdt2_dev

 

0

Sorry to belabor the point, but the Gradle console works. I can run it from the command line, or the Gradle tool window in IDEA (under Tasks, Other, console).

0

The exception is different this time - now all classes are found, but it fails on configuring grails datastore. Seems like a configuration problem. Please check this thread https://stackoverflow.com/q/44837752/2000323

0

I found that too, but I already have that line. Here's the whole build.grade. The only thing I added to the file since it was created by the generator was the postgresql line.

buildscript {
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.grails.plugins:hibernate5:${gormVersion-".RELEASE"}"
classpath "com.bertramlabs.plugins:asset-pipeline-gradle:2.14.8"
}
}

version "0.1"
group "cdt_dashboard"

apply plugin:"eclipse"
apply plugin:"idea"
apply plugin:"war"
apply plugin:"org.grails.grails-web"
apply plugin:"asset-pipeline"
apply plugin:"org.grails.grails-gsp"

repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}

dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.grails:grails-core"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-web-boot"
compile "org.grails:grails-logging"
compile "org.grails:grails-plugin-rest"
compile "org.grails:grails-plugin-databinding"
compile "org.grails:grails-plugin-i18n"
compile "org.grails:grails-plugin-services"
compile "org.grails:grails-plugin-url-mappings"
compile "org.grails:grails-plugin-interceptors"
compile "org.grails.plugins:cache"
compile "org.grails.plugins:async"
compile "org.grails.plugins:scaffolding"
compile "org.grails.plugins:events"
compile "org.grails.plugins:hibernate5"
compile "org.hibernate:hibernate-core:5.1.5.Final"
compile "org.grails.plugins:gsp"
console "org.grails:grails-console"
profile "org.grails.profiles:web"
runtime "org.glassfish.web:el-impl:2.1.2-b03"
runtime "com.h2database:h2"
runtime "org.apache.tomcat:tomcat-jdbc"
runtime "com.bertramlabs.plugins:asset-pipeline-grails:2.14.8"
runtime 'org.postgresql:postgresql:42.2.2'
testCompile "org.grails:grails-gorm-testing-support"
testCompile "org.grails:grails-web-testing-support"
testCompile "org.grails.plugins:geb:1.1.2"
testRuntime "org.seleniumhq.selenium:selenium-chrome-driver:2.47.1"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"
}

bootRun {
jvmArgs('-Dspring.output.ansi.enabled=always')
addResources = true
String springProfilesActive = 'spring.profiles.active'
systemProperty springProfilesActive, System.getProperty(springProfilesActive)
}

tasks.withType(Test) {
systemProperty "geb.env", System.getProperty('geb.env')
systemProperty "geb.build.reportsDir", reporting.file("geb/integrationTest")
systemProperty "webdriver.chrome.driver", System.getProperty('webdriver.chrome.driver')
systemProperty "webdriver.gecko.driver", System.getProperty('webdriver.gecko.driver')
}

assets {
minifyJs = true
minifyCss = true
}
0

In my Grails project, in the utils directory, I created a simple script to try to load some sample data into the project:

import org.apache.commons.csv.*
import cdm.Program

Reader asdf = new FileReader("workboard.csv")
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(asdf)
for (CSVRecord record in records) {
println(record.get(2));
if (record.get(2) != "") {
Program p = Program.create(name = record.get(2))
}
}

When I right-click on the script, and run it, I get the same error about GORM:

Caught: java.lang.IllegalStateException: Either class [cdm.Program] is not a domain class or GORM has not been initialized correctly or has already been shutdown.

As I've documented here, I can't fathom what else I would need to do to get the thing working correctly. It runs fine as a web application. I can create new Programs, and list them in the index page. It's only on the Groovy side that this is broken. Unfortunately, running scripts and having a console in the IDE was why I paid for the license. If I can't get these kinds of things working, I'm going to have to ask for my money back. Doesn't mean I'll be successful, but to me, that was supposed to be the advantage of IDEA over Eclipse or Netbeans: that it UNDERSTOOD the Grails stack, and could be an effective tool to help me develop in it. Please: what else can I try to get scripts/consoles connected in IDEA to the Grails application?

0

> It runs fine as a web application.

Do you mean the exact same code is executed correctly when you run it via Grails run configuration?

>I can't fathom what else I would need to do to get the thing working correctly.

As the error message says: the problem is either the class does not satisfy Gorm specification or the Gorm was not correctly configured and initialized before you run the program. What is the cdm.Program class definition? Are you sure it is gorm-compatible? Have you checked this thread: https://stackoverflow.com/a/35918765/2000323

I'd also suggest to ask the question on a Grails specific forums so that Grails community may provide you more ideas.

0

> Do you mean the exact same code is executed correctly when you run it via Grails run configuration?

YES! You finally see why this is so maddening! I can run the Grails configuration, and interact with the application with a browser, just fine! My database table is automatically created, and I can perform all the CRUD operations on my domain class via generated views. I can run `grails console' in a terminal, and I can even run the gradle task, "other," "console," and that works too. The problem is that IDEA ITSELF can't seem to sort out running a Groovy console or script. The actual Groovy/Gradle stuff seems to be fine.

0

>YES! You finally see why this is so maddening! I can run the Grails configuration, and interact with the application with a browser, just fine! 

What I mean is: do you able to call the exact same code, use the same class and same method? Thanks.

0

Please sign in to leave a comment.