Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to access current millisecond time in scala using java.lang.System.currentTimeMillis();

Tags:

scala

When I use below code in scala I get a runtime exception :

java.lang.NoSuchMethodError: main Exception in thread "main"

object Driver {

    def main(args:Array[String]) = {

      java.lang.System.currentTimeMillis();

    }

}

But when I remove java.lang.System.currentTimeMillis(); the main method is found.

Why is this ?

like image 884
blue-sky Avatar asked Dec 02 '22 20:12

blue-sky


2 Answers

It's the equal sign!

That's causing Scala to infer the return type of main to be Long (Scala) / long (at the JVM level). When you remove it, it infers Unit / void. Likewise when you remove the call to currentTimeMillis.

like image 65
Randall Schulz Avatar answered Dec 18 '22 11:12

Randall Schulz


def main(args:Array[String]): Unit = { is the exact signature for main(). Removing = seems also a solution, but less error-prone.

like image 39
idonnie Avatar answered Dec 18 '22 09:12

idonnie