Programming question: can import package in REPL, but not when running code

I’m trying to learn me some Scala. People probably have opinions about that but that is what is happening.

I don’t know if REPL is a term specific to Scala or not. By the term, I mean what happens when you just run scala.bat, and get a command line that allows you to enter scala code one line at a time.

In the REPL, if I type “import testpackage.TestClass”, it gives me no error message, and I am able then to type “val v = new TestClass” and everything works as expected.

However, if I create a .scala file with exactly the very same two lines, and try to run the .scala file from the shell using the command “scala filename.scala”, I receive an error message:

not found: value: testpackage

What could be happening here?

I’m not familiar with Scala, but executing most compilers/interpreters requires you to set paths in your environment so that the compiler/interpreter can find the various packages that you are trying to import.

I would guess that scala.bat sets those paths, and scala.exe does not.

I would suggest opening scala.bat in a text editor and seeing if it sets any environment variables, if it does then set those in your environment. Even better create a batch file which does the same thing as scala.bat, except instead of launching the command interpreter it executes scala.exe, so instead of calling scala.exe you would call myScala.bat which sets up your environment and then calls scala.exe.

Hope this helps.

Scala’s a bit goofy when it comes to importing things at the REPL. I’d suggest doing a scalac on TestClass, and then including the .class file by passing scala -i instead of relying on it automagically picking it up.

(I’m assuming here that the Windows version uses the same command line arguments as the Linux version, which is all I have available to me at work. Something like scala -i testpackage/TestClass.class Foo.scala works for me.)