This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting MethodMissingException on trying to run groovy class

 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll take the easy one first

Originally posted by Himanshu Gupta:

5.add(5)


Should be:



See the Number API.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

Can you help me in the error in compiling class also?
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you haven't started GroovyConsole before, just type groovyConsole in your command line. The GroovyConsole window will magically appear.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic