66

I'm trying to get started with Scala and cannot get out of the starting gate.

A file consisting of the line

package x

gives me

error: illegal start of definition

Regardless of what x is and regardless of where I put the file (I had a theory that I had to place the file in a directory hierarchy to match the package definition, but no). I get the same error with the example code from the web site and with the REPL.

Michael Lorton
  • 43,060
  • 26
  • 103
  • 144

5 Answers5

81

It looks like you're trying to declare the package membership in a Scala script (run using the scala command) or in the REPL.

Only files defining just classes and objects which are compiled with scalac may be defined as belonging to a package.

When you run code in a script or a REPL session, behind the scenes it is actually compiled inside a method of an object, in which scope a package declaration wouldn't be legal.

Ben James
  • 121,135
  • 26
  • 193
  • 155
  • 13
    So how then would you run the code from the 'examples' folder of the scala download archive? Most of them have a ``package examples`` line and the contents sticks to the rules I believe. I can ``scalac`` them, but running them with e.g. ``scala -classpath . sort`` gives me a ``java.lang.NoClassDefFoundError``!? – ThomasH Jun 20 '13 at 12:19
  • 1
    @ThomasH >scalac ScalaFileWithPackageLine.scala >scala fully.qualified.name.of.topLevel.ScalaObject – tdmadeeasy Dec 22 '15 at 19:15
27

Since Scala 2.11.0-M7 you can use :paste -raw (fix for issue SI-5299). This option allows defining packages in the REPL:

scala> :paste -raw
// Entering paste mode (ctrl-D to finish)

package Foo

class Bar

// Exiting paste mode, now interpreting.


scala> import Foo._
import Foo._

scala> new Bar
res1: Foo.Bar = Foo.Bar@3ee2cf81
qtwo
  • 497
  • 4
  • 10
3

I had the same problem. I've resolved it by importing import packageName._ instead of declaring a worksheet in the package.

dehasi
  • 2,644
  • 1
  • 19
  • 31
1

I had the same issue when I was executing scala program eg. "Game.scala" from terminal.

Compiling part was ok, error was shown when running the code, see below

☐ Wrong:

user@pc:~$scala Game.scala
/home/$USER/.../src/ul/org/bloxorz/Game.scala:1: error: illegal start of definition
package ul.org.bloxorz

Scala code should be invoked from terminal pretty much the same as Java code (you should give it a fully qualified class name and not the file name like I did in first example)

☑ Correct:

user@pc:~$scala ul.org.bloxorz.Game

Milan Bojovic
  • 199
  • 4
  • 10
-4

I don't get this error. How are you compiling this? And, by the way, what web site? As for REPL, it doesn't accept packages. Packages are only for compiled code.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • 3
    Wow, it literally *never* occurred to me that the compiler and the interpreter might have different command names. I was thinking of Python and using scala instead of scalac. "What web site?" -- I was about to answer http://www.scala-lang.org/ but I went and checked: the example code was from (the excellent) http://max-l.github.com/Squeryl/ (qv). You may know begin making those little doy, duh, durp noises you like so much. – Michael Lorton Apr 15 '10 at 17:53
  • I have same error: I'm in this folder: https://github.com/Sergey80/scala-samples/tree/master/src/main/scala/partial_function . trying do this: scala PartialToReal.scala – ses Mar 30 '14 at 00:18
  • 2
    This is more like a comment and not an answer. – David Griffin Feb 10 '16 at 19:35
  • @DavidGriffin It answered the REPL part, which was answerable. – Daniel C. Sobral Feb 10 '16 at 20:02