This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scripts not running inherited main method

11 replies
Rickard Nilsson
Joined: 2011-07-05,
User offline. Last seen 42 years 45 weeks ago.

Hi,

Given the following class:

class Foo {
def main(args: Array[String]) = {
println("Foo")
}
}

And the following script:

object Bar extends Foo

...running "scala Bar.scala" produces no output. Is this a bug or a
feature?

If I try adding a main method to Bar, then Scala tells me I need an
"override" directive, which makes sense. But shouldn't the script executer
also recognize the inherited main method?

Regards,
Rickard Nilsson

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scripts not running inherited main method

On 05/07/2011 00:37, Rickard Nilsson wrote:
> Hi,
>
> Given the following class:
>
> class Foo {
> def main(args: Array[String]) = {
> println("Foo")
> }
> }
>
> And the following script:
>
> object Bar extends Foo
>
> ...running "scala Bar.scala" produces no output. Is this a bug or a feature?
>
> If I try adding a main method to Bar, then Scala tells me I need an "override" directive,
> which makes sense. But shouldn't the script executer also recognize the inherited main
> method?

I am not expert, far from it; but if I try to run Foo directly, I get the error message:
java.lang.NoSuchMethodException: Foo.main is not static

So I am suspecting that main() isn't 'static' either for Bar, and thus not elected as
application entry point.
If I am way off base, I hope somebody more knowledgeable will correct me. ^_^'

Chris Marshall 2
Joined: 2011-05-26,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scripts not running inherited main method
Use:
object Foo {  def main(args : Array[String]) {  }}
instead
On Tue, Jul 5, 2011 at 11:30 AM, Philippe Lhoste <PhiLho@gmx.net> wrote:
On 05/07/2011 00:37, Rickard Nilsson wrote:
Hi,

Given the following class:

class Foo {
def main(args: Array[String]) = {
println("Foo")
}
}

And the following script:

object Bar extends Foo

...running "scala Bar.scala" produces no output. Is this a bug or a feature?

If I try adding a main method to Bar, then Scala tells me I need an "override" directive,
which makes sense. But shouldn't the script executer also recognize the inherited main
method?

I am not expert, far from it; but if I try to run Foo directly, I get the error message:
java.lang.NoSuchMethodException: Foo.main is not static

So I am suspecting that main() isn't 'static' either for Bar, and thus not elected as application entry point.
If I am way off base, I hope somebody more knowledgeable will correct me. ^_^'

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scripts not running inherited main method

On 05/07/2011 12:30, Philippe Lhoste wrote:
> If I am way off base, I hope somebody more knowledgeable will correct me. ^_^'

Wrong...
If I compile Bar, it works, so my assumption was just wrong.

Actually, it is puzzling.
If I have:
object Bar extends Foo
and run with:
scala FileWithBar.scala
I have no results, not even an error, even if Foo doesn't exist.
Is it defined somewhere in Scala?
Because if I rename Foo to Foof for example, I get an error...
FileWithBar.scala:1: error: not found: type Foof

Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scripts not running inherited main method

This works too:

class Foof {
def main(args: Array[String]) {
println("Foo")
}
}

object Bar extends Foof

Bar.main(null)

DaveScala
Joined: 2011-03-18,
User offline. Last seen 1 year 21 weeks ago.
Re: Scripts not running inherited main method

This oddity (that is: no output or output with null as argument) also
happens in the REPL so I guess it is a REPL/interpreter only bug
When compiled it works.

Peter C. Chapin 2
Joined: 2011-01-07,
User offline. Last seen 42 years 45 weeks ago.
RE: Re: Scripts not running inherited main method

My understanding is that scripts are wrapped in a main method automatically.
When you define your own class with a main method you are actually defining
a class inside main that happens to have another method named main. However,
the generated main method does not invoke those things and so nothing
happens. That is, when you write

class X {
def main(args: Array[String]) {
println("Hello")
}
}

It's really something like

class CompilerGeneratedThing {

def main(args: Array[String]) {
class X {
def main(args: Array[String]) {
println("Hello")
}
}

// No actions in main... no instance of X is made, no calls to any
methods. Nothing is done.
}

}

I could be wrong about this, but this is the mental model I've been using so
far. :)

Peter

> -----Original Message-----
> From: scala-language@googlegroups.com [mailto:scala-
> language@googlegroups.com] On Behalf Of Dave
> Sent: Tuesday, July 05, 2011 07:28
> To: scala-language
> Subject: [scala-language] Re: Scripts not running inherited main method
>
> This oddity (that is: no output or output with null as argument) also
happens
> in the REPL so I guess it is a REPL/interpreter only bug When compiled it
> works.

Todd Vierling
Joined: 2011-04-27,
User offline. Last seen 42 years 45 weeks ago.
Re: Scripts not running inherited main method
On Monday, July 4, 2011 6:37:35 PM UTC-4, Rickard Nilsson wrote:

Given the following class:

class Foo {
   def main(args: Array[String]) = {
     println("Foo")
   }
}

And the following script:

object Bar extends Foo

...running "scala Bar.scala" produces no output. Is this a bug or a  
feature?


If you want to run a main method on an object from the command line, you must compile it with scalac and run it from the .class file. main() is an entry point for precompiled obejcts, not for scripts. So, instead, do:
scalac Bar.scalascala Bar
Philippe Lhoste
Joined: 2010-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Scripts not running inherited main method

On 05/07/2011 15:14, Peter C. Chapin wrote:
> My understanding is that scripts are wrapped in a main method automatically.
> When you define your own class with a main method you are actually defining
> a class inside main that happens to have another method named main.

Good explanation, the previous results make sense, then (except the extends Foo working
without Foo).

Rickard Nilsson
Joined: 2011-07-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scripts not running inherited main method

On Tue, 5 Jul 2011 04:28:15 -0700 (PDT), Dave
wrote:
> This oddity (that is: no output or output with null as argument) also
> happens in the REPL so I guess it is a REPL/interpreter only bug
> When compiled it works.

Yes. I'll submit a bug report about this and see what happens. If
working, it would make running ScalaCheck tests directly from the
command line a bit more convenient.

/ Rickard

Rickard Nilsson
Joined: 2011-07-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scripts not running inherited main method

Den 2011-07-05 16:31:34 skrev Todd Vierling :

> On Monday, July 4, 2011 6:37:35 PM UTC-4, Rickard Nilsson wrote:
>>
>> Given the following class:
>>
>> class Foo {
>> def main(args: Array[String]) = {
>> println("Foo")
>> }
>> }
>>
>> And the following script:
>>
>> object Bar extends Foo
>>
>> ...running "scala Bar.scala" produces no output. Is this a bug or a
>> feature?
>>
>
> If you want to run a main method on an object from the command line, you
> must compile it with scalac and run it from the .class file. main() is an
> entry point for precompiled obejcts, not for scripts. So, instead, do:
>
> scalac Bar.scala
> scala Bar
>

No, since Scala 2.9 the script runner will run the main method. However,
it doesn't seem to look for a main-method in superclasses, which was my
trouble.

See the release notes for Scala 2.9:

"Scala code can now be executed in any of the following ways:
scala will run the main class, similar to java -jar
scala will run the main method of that class
scala will run the script contents as a scala script
scala will, if the contents are not a script, find a single
main method in a top level object and run that. This allows the same file
to be used with scalac and to be run directly.
scala -save will create a jar file with the compiled source,
which is then reusable and can be run as scala "

/ Rickard

Rickard Nilsson
Joined: 2011-07-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scripts not running inherited main method

Den 2011-07-05 15:14:45 skrev Peter C. Chapin :

> My understanding is that scripts are wrapped in a main method
> automatically.
> When you define your own class with a main method you are actually
> defining
> a class inside main that happens to have another method named main.
> However,
> the generated main method does not invoke those things and so nothing
> happens. That is, when you write
>
> class X {
> def main(args: Array[String]) {
> println("Hello")
> }
> }
>
> It's really something like
>
> class CompilerGeneratedThing {
>
> def main(args: Array[String]) {
> class X {
> def main(args: Array[String]) {
> println("Hello")
> }
> }
>
> // No actions in main... no instance of X is made, no calls to any
> methods. Nothing is done.
> }
>
> }
>
> I could be wrong about this, but this is the mental model I've been
> using so
> far. :)

Since Scala 2.9, the script runner will look for a main-method in top
level objects. You can run the following code as a script:

object Foo {
def main(args: Array[String]) = {
println("Hello")
}
}

It will output "Hello". However, main-methods in superclasses doesn't seem
to be found.

/ Rickard

>
> Peter
>
>> -----Original Message-----
>> From: scala-language@googlegroups.com [mailto:scala-
>> language@googlegroups.com] On Behalf Of Dave
>> Sent: Tuesday, July 05, 2011 07:28
>> To: scala-language
>> Subject: [scala-language] Re: Scripts not running inherited main method
>>
>> This oddity (that is: no output or output with null as argument) also
> happens
>> in the REPL so I guess it is a REPL/interpreter only bug When compiled
>> it
>> works.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland