Jakub Korab
Tech, Opinion, and Doing Stuff

NoSuchMethodException launching Scala App

January 16th, 2010

This is a pretty standard thing to do in Java:

public class App {
	public static void main(String[] args) {
		App app = new App();
		app.start();
	}

	public void start() {
		// do something
	}
}

So you reckon you’d be able to do this in Scala:

object App {
	def main(args: Array[String]) = {
		val app = new App()
		app.start()
	}
}

class App() {
	def start() = {
		// do something
	}
}

You go to run it and:
Exception in thread "main" java.lang.NoSuchMethodException: id.jakubkorab.App.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1605)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:107)

Turns out the Scala compiler doesn't treat application launching in the same way that it treats other classes with companion objects. If you rename the object App to another name, it works.

object App {
	def main(args: Array[String]) = {
		val app = new App()
		app.start()
	}
}

Go figure.


Filed under: scala | No Tag
No Tag
January 16th, 2010 18:01:35
2 comments

James Strachan
January 17, 2010

This is a side effect of how object’s get converted to bytecode. Since its a separate class in the JVM and if your object and class are the same names, the object class with the main has a different name (I think App$ though check).

Incidentally you mean “val app = new App()” – def actually defines a function which returns a new App; its just with the universal access principle it looks like a variable


Jake
January 17, 2010

You’re right, mistyped :) Corrected.

Leave a Reply