GeeCON Prague 2014*
The moose likes Groovy and the fly likes Getting MethodMissingException on trying to run groovy class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » Groovy
Bookmark "Getting MethodMissingException on trying to run groovy class" Watch "Getting MethodMissingException on trying to run groovy class" New topic
Author

Getting MethodMissingException on trying to run groovy class

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Whenever I am trying to Compile the groovy class I am getting this error.

My groovy class is :

class Book {
private String title
Book (String theTitle) {
title = theTitle }

String getTitle(){
return title
}
}

and the console error is :

groovy.lang.GroovyRuntimeException: This script or class could not be run.
It should either:
- have a main method,
- be a JUnit test, TestNG test or extend GroovyTestCase,
- or implement the Runnable interface.


Please help to solve this error.


My Blog SCJP 5 SCWCD 5
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

same error is coming when I am executing this functin

5.add(5)

But sometimes it does not show error.

Dont know what is happening?




[ ]
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
I'll take the easy one first

Originally posted by Himanshu Gupta:

5.add(5)


Should be:



See the Number API.


Grails Consultant
http://dangertree.net
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks.

Can you help me in the error in compiling class also?
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
Originally posted by Himanshu Gupta:



This looks too much like Java code! Try something more like this:



Specifically, there are two Groovy language features that you should take advantage of.

1. Automatic getters and setters, or Groovy Beans. No need to defined those methods, or call them. Groovy provides them for you.

2. In Groovy, there is a feature called named parameters. This allows you to pass the names of the properties you want to set into the constructor of an object. You can do this with any method, even when calling a Java api.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Sorry for troubling you again but now I am getting this error after changing the code:

Exception thrown: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, /C:/Documents%20and%20Settings/u17793/Desktop/groovy-binary-1.6-beta-2/groovy-1.6-beta-2/bin/Book.groovy: 1: Invalid duplicate class definition of class Book : The source /C:/Documents%20and%20Settings/u17793/Desktop/groovy-binary-1.6-beta-2/groovy-1.6-beta-2/bin/Book.groovy contains at least two defintions of the class Book.
One of the classes is a explicit generated class using the class statement, the other is a class generated from the script body based on the file name. Solutions are to change the file name or to change the class name.
@ line 1, column 1.
class Book {
^

1 error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, /C:/Documents%20and%20Settings/u17793/Desktop/groovy-binary-1.6-beta-2/groovy-1.6-beta-2/bin/Book.groovy: 1: Invalid duplicate class definition of class Book : The source /C:/Documents%20and%20Settings/u17793/Desktop/groovy-binary-1.6-beta-2/groovy-1.6-beta-2/bin/Book.groovy contains at least two defintions of the class Book.
One of the classes is a explicit generated class using the class statement, the other is a class generated from the script body based on the file name. Solutions are to change the file name or to change the class name.
@ line 1, column 1.
class Book {
^

1 error
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
If you have a file called "Book.groovy" with this code:



And another file called "makeBook.groovy" with this code:



And you execute this from your command line:


You should see this:



Does that work?
[ October 15, 2008: Message edited by: Matthew Taylor ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

If you run your code in GroovyConsole, you don't have to put it in two separate files. Just paste the whole thing in there and hit Ctrl+Enter to run it.

It's soooooo much faster!


A good workman is known by his tools.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

If you haven't started GroovyConsole before, just type groovyConsole in your command line. The GroovyConsole window will magically appear.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks

It worked by putting all code in console and running it.


I want to learn making class file which will have methods and classes and then using it with normal Java code.


 
GeeCON Prague 2014
 
subject: Getting MethodMissingException on trying to run groovy class