Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main class with companion cannot be run #4861

Closed
scabug opened this issue Aug 1, 2011 · 5 comments
Closed

Main class with companion cannot be run #4861

scabug opened this issue Aug 1, 2011 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Aug 1, 2011

==========================================================================================
This is a bug, affected versions (at least) 2.9.0.1, 2.9.1.RC1 and 2.10 (a recent nightly)

This code compiles:

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

But it won't run:

java.lang.NoSuchMethodException: Foo.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1605)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:74)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

@scabug
Copy link
Author

scabug commented Aug 1, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4861?orig=1
Reporter: @hseeberger
Affected Versions: 2.9.0, 2.9.1

@scabug
Copy link
Author

scabug commented Aug 1, 2011

@SethTisue said:
Should work if you change "trait Foo" to "class Foo".

From Java, a Scala trait is an interface, not a class, and interfaces can't have static methods such as "main". So this is unfixable.

@scabug
Copy link
Author

scabug commented Aug 1, 2011

@paulp said:
Thank you for saving me the trouble of figuring out/remembering that -- I'm reopening because I can recognize the attempt and issue a warning.

@scabug
Copy link
Author

scabug commented Aug 1, 2011

@hseeberger said:
Seth: Thanks for the explanation. Now that I know it it's obvious ;-)
Paul: A warning would be great.

@scabug
Copy link
Author

scabug commented Aug 1, 2011

Commit Message Bot (anonymous) said:
(extempore in r25427) Working on jar creation infrastructure. Now if -d foo.jar is used for
output generation (but only then, since otherwise we're not creating the jar):

  1. -Xmain-class foo.Bar will give the jar a Main-Class of foo.Bar
  2. Alternatively, if there is only one runnable program, that will be the Main-Class
  3. Always, the jar's manifest will have an entry for Scala-Compiler-Version.

Not very relatedly, a warning is now issued when a module has a main method but
a runnable program will not be generated. Closes #4861.

This represents an opening step toward automatically recognizing mismatched
bytecode situations: coarse, but useful and safe. Review by mirco.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants