1

I have a Scala project that I want to run from the command line. However, this is within a package so if I try to run the class Main(which contains a main method) I get:

error: illegal start of definition
package mypackage

Any idea how to run Main from the command line while still keeping the package?

bsky
  • 19,326
  • 49
  • 155
  • 270

2 Answers2

1

If sbt run works as you say, sbt console is what you are looking for.

Once you are in the sbt console, execute your main method by typing

mypackage.Main.main()
  • If I run `mypackage.Main.main(Array.empty)` then I don't get a user console. – bsky May 04 '17 at 10:36
  • Is there an infinite loop in your main function? –  May 04 '17 at 10:51
  • Yes, there is one which asks for user input. – bsky May 04 '17 at 10:53
  • How exactly do you open the user console? –  May 04 '17 at 10:55
  • `while(true) { println("something") /*some other code*/ scanner.nextLine() /*some other code*/ }` – bsky May 04 '17 at 10:57
  • Here's an example how you can use Scanner to read input. Note that you have to press return twice when you give the input in sbt console. https://i.imgur.com/RhXDF7N.png Perhaps your Scanner is not reading the input from System.in? –  May 04 '17 at 11:06
0

First sbt and when you're in the console mode (i.e. right after you hit enter) runMain Main.

That always worked for me.

vasigorc
  • 882
  • 11
  • 22
  • If I do that, I get `java.lang.ClassNotFoundException: Main`(the class is not seen even though it exists). – bsky May 04 '17 at 10:24
  • The same happens for `runMain mypackage.Main`? If you do `sbt` then `compile` do you get a success message? – vasigorc May 05 '17 at 05:20