i've been playing around with this code for a couple of days now trying to get it to work in a self-contained jar file (since i eventually want to use the code to access gorm from java) - the problem i run into is that the dynamic gorm 'save()' method isn't found at runtime (see stack trace below) - the problem disappears if i include the 'grails-app' directory containing the domain class definitions in the same directory as the jar file containing the compiled class files
my jar file ('beckwith.jar') includes all the class files generated by 'grails compile' as well as the 'grails-app' directory contents and the 'application.properties' file - at the moment i have to include the file 'scripts/Main.groovy' in the same directory as the jar file until i figure out how to load it as a resource from the jar file
here's the command i'm using to run the program
java -classpath beckwith.jar:$GRAILS_HOME/lib/groovy-all-1.5.6.jar:$GRAILS_HOME/lib/spring-2.5.4.jar:$GRAILS_HOME/lib/commons-logging-1.1.jar:$GRAILS_HOME/dist/grails-core-1.0.3.jar:$GRAILS_HOME/lib/javaee.jar:$GRAILS_HOME/dist/grails-spring-1.0.3.jar:$GRAILS_HOME/dist/grails-gorm-1.0.3.jar:$GRAILS_HOME/lib/hibernate-annotations.jar:$GRAILS_HOME/lib/hibernate3.jar:$GRAILS_HOME/lib/dom4j-1.6.1.jar:$GRAILS_HOME/lib/hibernate-commons-annotations.jar:.:$GRAILS_HOME/lib/commons-lang-2.1.jar:$GRAILS_HOME/lib/commons-collections-3.2.jar:lib/mysql-connector-java-5.0.8-bin.jar:$GRAILS_HOME/lib/cglib-nodep-2.1_3.jar Tester
here's the exception i get - any idea how to get around this?
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: com.burtbeckwith.gorm.domain.Book.save() is applicable for argument types: () values: {}
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:60)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:172)
at com.burtbeckwith.gorm.domain.Book.invokeMethod(Book.groovy)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:784)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethod0(ScriptBytecodeAdapter.java:198)
at Main.run(Main.groovy:7)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:473)
at groovy.lang.GroovyShell.evaluate(GroovyShell.java:508)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at Tester$_main_closure1.doCall(Tester.groovy:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at groovy.lang.Closure.call(Closure.java:292)
at groovy.lang.Closure.call(Closure.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:778)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:758)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at com.burtbeckwith.gorm.GormHelper$_withTransaction_closure1.doCall(GormHelper.groovy:133)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at groovy.lang.Closure.call(Closure.java:292)
at org.codehaus.groovy.runtime.ConvertedClosure.invokeCustom(ConvertedClosure.java:48)
at org.codehaus.groovy.runtime.ConversionHandler.invoke(ConversionHandler.java:72)
at $Proxy0.doInTransaction(Unknown Source)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:912)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:946)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:756)
at org.codehaus.groovy.runtime.InvokerHelper.invokePojoMethod(InvokerHelper.java:766)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:754)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at com.burtbeckwith.gorm.GormHelper.withTransaction(GormHelper.groovy:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:86)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:230)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1105)
at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:749)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodN(ScriptBytecodeAdapter.java:170)
at Tester.main(Tester.groovy:10)
burtbeckwith wrote:
I spent some time on this and got it working. I've posted a writeup and a zip
file here:
http://burtbeckwith.com/blog/?p=66Burt
On Saturday 23 August 2008 3:59:22 pm Christopher Mason wrote:
> Clarifying:
>
> So there are two projects that purport to allow one to use grails
> without having a web server around:
>
>
http://docs.codehaus.org/display/GRAILS/GORM+-+StandAlone+Gorm> which is based on:
>
http://jweldin.com/blog/?p=6>
> However, both are over a year old and don't compile. Both use this
> constructor:
>
> application =
> new DefaultGrailsApplication( groovyFiles, new
> GormInjectionOperation() )
>
> which, it seems, no longer exists.
>
> My suspicion is that this configuration is all done now via Spring. Is
> this true?
>
> I'm trying to read through the grails Gant scripts source to figure out
> how grails starts up, but it's not clear yet. I've tried various
> methods on GrailsUtil like:
>
> GrailsUtil.bootstrapGrailsFromApplication(grailsApplication)
> or
> ApplicationContext appContext = GrailsUtil
> .bootstrapGrailsFromClassPath()
> DefaultGrailsApplication grailsApplication =
> (DefaultGrailsApplication)appContext
> .getBean("grailsApplication", D
> efaultGrailsApplication.class);
>
> but this seems to result in class not found errors, I assume because the
> Gant scripts are responsible for classpathy stuff that I'm not yet doing
> (eg Init.groovy:setClasspath()). Am I headed in the right direction?
>
> If I simply omit the GormInjectionOperation from the code posted above,
> obviously, none of the GORM methods are available on doman objects.
>
> Any hints would be most appreciated. My goal is something I can run via
> java -jar that will execute a groovy script using GORM domain classes.
>
> I offer beer to anyone who can help me get this working! And I promise
> to post it somewhere for all to read!
>
> -c
>
> Christopher Mason wrote:
> > Can anyone help update this code for grails 1.0.3?
> >
> >
http://jweldin.com/blog/?p=6> >
> > Does anyone have this working? (I see several posts but none of them
> > seem to have successful resolutions.)
> >
> > Thanks so much!
> >
> > -c
> >
> >
> > import groovy.lang.GroovyClassLoader;
> > import groovy.lang.GroovyObject;
> > import org.codehaus.groovy.grails.commons.*;
> > import org.codehaus.groovy.grails.orm.hibernate.cfg.*;
> > import org.hibernate.SessionFactory;
> > import org.springframework.core.io.*;
> > import org.springframework.core.io.support.*
> > import
> > org.codehaus.groovy.grails.compiler.injection.GrailsAwareClassLoader
> > import org.codehaus.groovy.grails.compiler.injection.ClassInjector;
> >
> > class RunScript
> > {
> > public static void main(String[] args)
> > {
> >
> > if (args.length < 2) {
> > println "Usage: java -jar profiler-script.jar <db.props>
> > <script name> <script args>"
> > }
> >
> > GrailsApplication grailsApplication = null;
> > SessionFactory sessionFactory = null;
> >
> > PathMatchingResourcePatternResolver resolver = new
> > PathMatchingResourcePatternResolver();
> >
> > Resource[] groovyFiles =
> > resolver.getResources("classpath*:**grails-app/**/*.groovy");
> >
> > grailsApplication = new DefaultGrailsApplication(groovyFiles);
> >
> > GrailsAwareClassLoader gcl = new GrailsAwareClassLoader()
> > gcl.setClassInjectors([ new DefaultGrailsDomainClassInjector() ]
> > as ClassInjector[])
> >
> > DefaultGrailsDomainConfiguration config = new
> > DefaultGrailsDomainConfiguration();
> > config.setGrailsApplication(grailsApplication);
> >
> > Properties props = new Properties();
> > props.load(new FileInputStream(args[0]));
> > config.setProperties(props);
> >
> > sessionFactory = config.buildSessionFactory();
> >
> > GrailsHibernateUtil.configureDynamicMethods(sessionFactory,
> > grailsApplication);
> >
> > GroovyClassLoader cl = gcl //grailsApplication.getClassLoader()
> >
> > println "\n-----------\nBefore Main\n-----------\n"
> >
> > Binding b = new Binding(args[3..args.length-1] as String[])
> > def TestDomainScript =
> > (Script)cl.loadClass(args[1],false,true).newInstance(b)
> > TestDomainScript.run()
> >
> > println "\n-----------\nAfter Main\n-----------\n"
> > println "\nDone";
> >
> > }
> > }
> >
> >
> > import
> > org.codehaus.groovy.grails.compiler.injection.GrailsInjectionOperation;
> > import org.codehaus.groovy.grails.compiler.injection.*;
> > import org.apache.commons.logging.Log;
> > import org.apache.commons.logging.LogFactory;
> > import org.codehaus.groovy.ast.ClassNode;
> > import org.codehaus.groovy.classgen.GeneratorContext;
> > import org.codehaus.groovy.control.CompilationFailedException;
> > import org.codehaus.groovy.control.CompilationUnit;
> > import org.codehaus.groovy.control.Phases;
> > import org.codehaus.groovy.control.SourceUnit;
> > import org.codehaus.groovy.grails.compiler.support.GrailsResourceLoader;
> > import org.codehaus.groovy.grails.commons.GrailsResourceUtils
> > import org.codehaus.groovy.grails.compiler.GrailsClassLoader;
> >
> > public class MyInjectionOperation extends GrailsInjectionOperation
> > {
> >
> > private static GrailsDomainClassInjector injector = new
> > DefaultGrailsDomainClassInjector();;
> > private GrailsResourceLoader resourceLoader;
> >
> > public void call(SourceUnit source, GeneratorContext context,
> > ClassNode classNode) throws CompilationFailedException
> > {
> >
> > if(resourceLoader != null)
> > {
> > try {
> > URL url;
> > if(GrailsResourceUtils.isGrailsPath(source.getName()))
> > {
> > url =
> > resourceLoader.loadGroovySource(GrailsResourceUtils.getClassName(source.g
> >etName()));
> >
> > }
> > else
> > {
> > url =
> > resourceLoader.loadGroovySource(source.getName());
> > }
> > if(GrailsResourceUtils.isDomainClass(url))
> > {
> >
> > this.injector.performInjection(source,context,classNode);
> > }
> >
> > }
> > catch (MalformedURLException e)
> > {
> > println("Error loading URL during addition of compile
> > time properties: " + e.getMessage(),e);
> > throw new
> > CompilationFailedException(Phases.CONVERSION,source,e);
> > }
> > }
> > }
> >
> > public void setResourceLoader(GrailsResourceLoader resourceLoader)
> > {
> > this.resourceLoader = resourceLoader;
> > }
> > }
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
> >
> >
http://xircles.codehaus.org/manage_email>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>
http://xircles.codehaus.org/manage_email---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email