Error: not found: type FunSuite

Is there a way from the scalac command line (from UNIX) do something so that the compiler can find org.scalatest ? Hopefully the answer is to set an environment variable or include some flag on the command line. When I try to compile a particular file form the command line, I get the following error messages:

GreetingTestSuite.scala:1: error: object scalatest is not a member of package org
import org.scalatest._
           ^
GreetingTestSuite.scala:4: error: not found: type FunSuite
class GreetingTestSuite extends FunSuite {
                                ^
GreetingTestSuite.scala:6: error: not found: value test
  test("greeting") {
  ^
GreetingTestSuite.scala:11: error: not found: value test
  test("HW customized greeting") {
  ^
four errors found

scalac -classpath location/*.jar

scalac without anything else will give you all options.

@martijnhoekstra are you sure about the syntax? location/*.jar will expand into a space separated string of file names. The documentation seems to indicate that it is a : separated list of paths, not space separated.

When I look at the documentation you suggest (the simple output of the scalac with no command line flags), I see the following.

  -classpath <path>                    Specify where to find user class files.

The manual page has the following.

       -classpath <path>
              Specify where to find user class files (on Unix-based systems a colon-separated list of paths,  on  Windows-
              based  systems,  a  semicolon-separated  list of paths). This does not override the built-in ("boot") search
              path.

              The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath com-
              mand-line option overrides that default, so if you want to include the current directory in the search path,
              you must include "." in the new settings.

It does not specify whether that is the path to a directory (directories) or the path to a .jar file (jar files).

BTW how can I find the jar that includes org.scalatest ?

It’s a pattern, so dir/subdir/*.jar for all jars in dir/subdir

The jar that inlcudes org.scalatest is the one you downloaded from scalatest. Usually your build tool will download and manage your dependencies for you, and place them on the classpath for scalac, but if you’re doing it by hand, you really need to do it all by hand.

I’m not sure if you are using windows or unix, but in unix dir/subdir/*.jar is not a pattern that the program reads, rather it is a pattern the shell expands into a space separated sequence of file names for all the files in that directory which have a .jar extension. Besides, the documentation says it is a “colon separated list of paths”, so according to the documentation it is NOT a pattern.

That subtlety aside, how to figure out which .jar defines scalatest? For example, can I ask IntelliJ somehow? Am I correct in supposing that the name of the file might or might not include the word “scalatest” ?

http://central.maven.org/maven2/org/scalatest/scalatest_2.12/3.0.7/scalatest_2.12-3.0.7.jar

Thanks for the URL. I downloaded the file into a directory ./jars, and
I’m still getting the following error

scalac -classpath .:jars GreetingTestSuite.scala
GreetingTestSuite.scala:1: error: object scalatest is not a member of package org
import org.scalatest._
           ^
GreetingTestSuite.scala:4: error: not found: type FunSuite
class GreetingTestSuite extends FunSuite {
                                ^
GreetingTestSuite.scala:6: error: not found: value test
  test("greeting") {
  ^
GreetingTestSuite.scala:11: error: not found: value test
  test("HW customized greeting") {
  ^
four errors found

What am I missing?

Just to underscore this: you’re basically trying to replace a whole raft of features that sbt provides, including taking a standard Maven artifact path, resolve that, find the .jar, download it, and put it into the right place – times all the transitive dependencies that depends on. It’s very unusual to try to do all this by hand, precisely because it’s a huge pain…

When I try to use sbt in batch from the unix shell, I get what seems like the same error.

sh> sbt compile test
[info] Updated file /private/tmp/jimka/scalain_e/22857/project/build.properties: set sbt.version to 1.2.8
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from /Users/jimka/.sbt/1.0/plugins
[info] Loading project definition from /private/tmp/jimka/scalain_e/22857/project
[info] Updating ProjectRef(uri("file:/private/tmp/jimka/scalain_e/22857/project/"), "root-22857-build")...
[info] Done updating.
[info] Loading settings for project root-22857 from build.sbt ...
[info] Set current project to scalain-e-course-code (in build file:/private/tmp/jimka/scalain_e/22857/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] Updating ...
[info] Done updating.
[info] Compiling 3 Scala sources to /private/tmp/jimka/scalain_e/22857/target/scala-2.12/classes ...
[error] /private/tmp/jimka/scalain_e/22857/GreetingTestSuite.scala:1:12: object scalatest is not a member of package org
[error] import org.scalatest._
[error]            ^
[error] /private/tmp/jimka/scalain_e/22857/GreetingTestSuite.scala:4:33: not found: type FunSuite
[error] class GreetingTestSuite extends FunSuite {
[error]                                 ^
[error] /private/tmp/jimka/scalain_e/22857/GreetingTestSuite.scala:6:3: not found: value test
[error]   test("greeting") {
[error]   ^
[error] /private/tmp/jimka/scalain_e/22857/GreetingTestSuite.scala:11:3: not found: value test
[error]   test("HW customized greeting") {
[error]   ^
[error] four errors found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 4 s, completed May 11, 2019 12:00:05 PM

Are your tests in src/main/scala or src/test/scala? If they are in src/main/scala, and your build.sbt is pulling in scalatest as a test-scoped dependency, you would see an error like this.

You will want to make sure your project is laid out correctly - the sbt docs do a decent job explaining it.

Thanks @wrbriggs for the clue. I had already discovered more or less this. It turns out sbt lets me put the source code anywhere. It does not have to be in src/main/scala and it compiles just fine. However, and bizarrely, sbt seems to REQUIRE that the tests be in src/test/scala. I’ve now updated my shell script to create the required(recommended?) directories ‘src/{main,test}/scala’ and populate them with the corresponding files.

Now I see beautiful output.

sh> sbt compile "testOnly GreetingTestSuite"
[info] Updated file /private/tmp/jimka/scalain_e/24693/project/build.properties: set sbt.version to 1.2.8
[info] Loading settings for project global-plugins from idea.sbt ...
[info] Loading global plugins from /Users/jimka/.sbt/1.0/plugins
[info] Loading project definition from /private/tmp/jimka/scalain_e/24693/project
[info] Updating ProjectRef(uri("file:/private/tmp/jimka/scalain_e/24693/project/"), "root-24693-build")...
[info] Done updating.
[info] Loading settings for project root-24693 from build.sbt ...
[info] Set current project to greeting (in build file:/private/tmp/jimka/scalain_e/24693/)
[info] Executing in batch mode. For better performance use sbt's shell
[info] Updating ...
[info] Done updating.
[info] Compiling 2 Scala sources to /private/tmp/jimka/scalain_e/24693/target/scala-2.12/classes ...
[info] Done compiling.
[success] Total time: 6 s, completed May 11, 2019 12:31:55 PM
[info] Compiling 1 Scala source to /private/tmp/jimka/scalain_e/24693/target/scala-2.12/test-classes ...
[info] Done compiling.
hello world 
hello jim!
[info] GreetingTestSuite:
[info] - greeting
[info] - HW customized greeting
[info] ScalaTest
[info] Run completed in 676 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 4 s, completed May 11, 2019 12:32:00 PM