I just started learning Scala, and as part of the process, I’ve been trying to write some simple scripts that use Swing.
Here is an extremely stripped down example which exhibits the problem that I am seeing.
SimpleSwingApp:
import scala.swing._
object SimpleSwingApp extends SimpleSwingApplication {
def top = new MainFrame {
println ("Starting")
title = "First Swing App"
contents = new Button { text = "Click me" }
}
}
When I run the script through the Eclipse Scala IDE as a Scala Application, everything works as expected. I get the expected “Starting” text printed to the Eclipse console and the little GUI pops up and waits for a click.
When I try to run it as a script from the command line, I get nothing.
> scala SimpleSwingApp.scala
No error message, no “Starting”, no GUI.
So what is going on, and how can I achieve the result that I want (ie, starting a Scala GUI script from the command line)?
The problem is that the Scala script runner is looking for a main method in your script file, but it isn’t finding one.
The reason it isn’t finding one is because you are inheriting main from SimpleSwingApplication, so there isn’t a main method in your script file.
You can work around the problem by adding a main method to your script that immediately invokes super.main, as follows:
Now, the script will not raise an error in the Scala IDE, and it can be executed by calling
rather than the less aesthetic