main method in file with class and object?

View: New views
6 Messages — Rating Filter:   Alert me  

main method in file with class and object?

by John Tyler-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello, I'm trying to verify some behavior I've seen.  I defined a file MyApp.scala as such:
class MyApp {
  // blah
}

object MyApp {
  def main(args: Array[String]) {
    val app = new MyApp
    // blah
  }
}

I then compiled with "scalac MyApp.scala", but when I run with "scala MyApp" from the same directory I get:
java.lang.NoSuchMethodException: MyApp.main([Ljava.lang.String ;)

The only way I've been able to get this application to start is by declaring the object MyApp in a separate file with nothing else in it.  (I want to use command-line arguments so haven't tried this particular case with object MyApp extends Application.)  I've checked the ScalaReference.pdf, "Programming in Scala" book from Odersky et al, this mailing list's archives, and Google for some confirmation that this is indeed required but so far no luck.  In fact I have found a number of places mention something similar to this excerpt from "Programming in Scala":
Any singleton object with a main method of the proper signature can be used as the entry point into an application.

My Java and Scala versions are below, I'm running Ubuntu 7.10.  Any ideas?

Thanks!
John

java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)

Scala code runner version 2.6.1-final -- (c) 2002-2007 LAMP/EPFL


Re: main method in file with class and object?

by Eric Willigers :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

John Tyler wrote:

> Hello, I'm trying to verify some behavior I've seen.  I defined a file
> MyApp.scala as such:
> class MyApp {
>   // blah
> }
>
> object MyApp {
>   def main(args: Array[String]) {
>     val app = new MyApp
>     // blah
>   }
> }
>
> I then compiled with "scalac MyApp.scala", but when I run with "scala
> MyApp" from the same directory I get:
> java.lang.NoSuchMethodException: MyApp.main([Ljava.lang.String ;)


You might choose to explicitly declare that def main returns type Unit

But the problem you are hitting is that you have both a class MyApp and
an object MyApp. main won't work in such cases. Rename the class MyApp
and you'll be fine.   You'll see that object MyApp compiles to
MyApp.class and MyApp$.class while class MyApp also compiles to
MyApp.class - a collision. Class MyApp dominates instead of there being
a merge (which might be hard with incremental compiling, separate
compiling, etc).


Re: Re: main method in file with class and object?

by Bill Venners :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Eric,

On Jan 16, 2008, at 11:18 PM, Eric Willigers wrote:

> John Tyler wrote:
>> Hello, I'm trying to verify some behavior I've seen.  I defined a  
>> file MyApp.scala as such:
>> class MyApp {
>>  // blah
>> }
>> object MyApp {
>>  def main(args: Array[String]) {
>>    val app = new MyApp
>>    // blah
>>  }
>> }
>> I then compiled with "scalac MyApp.scala", but when I run with  
>> "scala MyApp" from the same directory I get:
>> java.lang.NoSuchMethodException: MyApp.main([Ljava.lang.String ;)
>
>
> You might choose to explicitly declare that def main returns type Unit
>
> But the problem you are hitting is that you have both a class MyApp  
> and an object MyApp. main won't work in such cases. Rename the class  
> MyApp and you'll be fine.   You'll see that object MyApp compiles to  
> MyApp.class and MyApp$.class while class MyApp also compiles to  
> MyApp.class - a collision. Class MyApp dominates instead of there  
> being a merge (which might be hard with incremental compiling,  
> separate compiling, etc).
>
In which case I need to update the book because it is wrong. It should  
say that any *standalone object* with a main of the proper signature  
can be used as an entry point into an application. MyApp$ has a main  
method, but it isn't static. I had never attempted to make an  
application with anything other than a standalone object.

Bill

Re: main method in file with class and object?

by Arrgh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 16-Jan-08, at 8:05 PM, John Tyler wrote:
I then compiled with "scalac MyApp.scala", but when I run with "scala MyApp" from the same directory I get:
java.lang.NoSuchMethodException: MyApp.main([Ljava.lang.String ;)


:)

-0xe1a

Re: main method in file with class and object?

by Bill Venners :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Alex,

On Jan 16, 2008, at 11:36 PM, Alex Cruise wrote:

> On 16-Jan-08, at 8:05 PM, John Tyler wrote:
>> I then compiled with "scalac MyApp.scala", but when I run with  
>> "scala MyApp" from the same directory I get:
>> java.lang.NoSuchMethodException: MyApp.main([Ljava.lang.String ;)
>
> http://lampsvn.epfl.ch/trac/scala/ticket/363
>
>
In which case I'll hold my horses until it is sorted out.

Bill

Re: Re: main method in file with class and object?

by Alex Blewitt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Jan 17, 2008 4:25 AM, Bill Venners <bv-jackrabbit@...> wrote:

>
> > But the problem you are hitting is that you have both a class MyApp
> > and an object MyApp. main won't work in such cases. Rename the class
> > MyApp and you'll be fine.   You'll see that object MyApp compiles to
> > MyApp.class and MyApp$.class while class MyApp also compiles to
> > MyApp.class - a collision. Class MyApp dominates instead of there
> > being a merge (which might be hard with incremental compiling,
> > separate compiling, etc).
> >
> In which case I need to update the book because it is wrong. It should
> say that any *standalone object* with a main of the proper signature
> can be used as an entry point into an application.

Isn't the problem not that it's a standalone object, but there's a
collision between an object-called-MyApp and class-called-MyApp? You
could mix other objects in there as well, but the name of the
application object should be unique.

Alex