3

I run Windows 7.

I have java (a current enough version to run scala) and scala downloaded on my computer. I've set PATH so that when I type "scala" into the command prompt it sends me to the proper interface:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51) Type in expressions to have them evaluated. Type :help for more information

However I can't execute the command "scala" or "scalac" on my test files.

scala> scala Hello
(console):8: error: object Hello is not a member of package scala

This makes me think I'm in the wrong directory. The file Hello.scala is saved in the home directory that I set PATH to.

However I get a different issue when I try to compile code.

scala> scalac Hello.scala
(console):1: error: ';' expected but '.' found.

I actually got my test file to work at one point... but I wasn't actually IN scala.

C:\scala-2.9.1.final\bin> scala Hello.scala
Hello world!

I'm not really sure how to proceed from here. If anyone has any ideas of what may be wrong I would greatly appreciate input.

2 Answers 2

5

It appears that you're trying to run & compile programs from within the Scala REPL (read-evaluate-print loop - a kind of Scala interpreter) and you cannot do that in the REPL. The REPL allows you to type in Scala statements and see them execute immediately. (If you're not sure how you entered the REPL, you probably just entered the command scala from the command line.) The REPL is useful for testing ideas, and for experimenting with Scala. For example:

C:\some\path> scala
Welcome to Scala version 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_91).
Type in expressions for evaluation. Or try :help.

scala> println ("Hello!")
Hello!

scala> val x = 10
x: Int = 10

scala> val y = x * 5
y: Int = 50

scala> sys.exit
C:\some\path>

However, the REPL isn't what you would use to compile & run Scala programs - you need to do that from the command line (or from a tool such as sbt). If you want to run your program directly from the command line, without using the REPL (that is, without being in Scala, as you put it) then you would need to do the following:

Firstly, compile your program using scalac:

C:\some\path> scalac Hello.scala

If that succeeds, you can then run the program with the scala command (which looks for a Hello.class file):

C:\some\path> scala Hello

(Here C:\some\path is the location of the files Hello.scala & Hello.class.)

Alternatively, as you have already discovered, you can run your Scala program as a script in the REPL. You can do this from the command line as follows (note the addition of the filetype .scala after Hello compared to the command above):

C:\some\path> scala Hello.scala

or from within the REPL:

scala> :load Hello.scala

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to issue scala command when you're inside REPL. If you want to execute code from that file, load it:

here is what I have in Foo.scala

println("I'm foo") 

Now I'm starting the REPL (and as you can see scala> is a sign that you're ALREADY into REPL and can start execute raw scala code):

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load Foo.scala
Loading Foo.scala...
I'm foo

2 Comments

I also tried using it on my file but didn't work, said the file doesn't exist. Maybe I'm in the wrong directory?
To get help on the load command, type :help load rather than :help :load (the colon is the prefix for a command, to distinguish it from a Scala statement).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.