0

I am learning to program in Scala. I have two packages named chapter3 and chapter4. Below is the code:

Code for the file FileOperation.scala in package chapter3:

package chapter3

import scala.io.Source

object FileOperation {

  /**
   * This function determines the length of line length.
   */
  def findLineLengthWidth(line : String) : Int = {
    val len = line.length.toString.length()
    return len;
  }

  def readFile(filename : String) {
    val lines = Source.fromFile(filename).getLines().toList
    val longestLine = lines.reduceLeft((a, b) => if(a.length > b.length) a else b)
    val maxlength = findLineLengthWidth(longestLine)

    for (line <- lines) {
      val len = findLineLengthWidth(line)
      val spacecount = (maxlength - len)
      val padding = " " * spacecount
      println(padding + line.length +"|"+ line)
    }
  }
}

code for file in chapter4 package: Summer.scala

package chapter4

import chapter3.FileOperation._

object Summer {

  def main(args: Array[String]): Unit = {
    {
      //val file = new FileOperation
      readFile("abc.txt")
    } 
  }
}

When I run this code in Eclipse, it works fine. However when I try to compile it in the terminal, I get the following error:

$ scalac *.scala
Summer.scala:3: error: not found: object chapter3
import chapter3.FileOperation._
       ^
Summer.scala:11: error: not found: value readFile
      readFile("abc.txt")
      ^
two errors found

1 Answer 1

1

Make sure that your directory structure is:

chapter3/FileOperation.scala
chapter4/Summer.scala

Then, from the parent directory run:

scalac chapter3/FileOperation.scala chapter4/Summer.scala

This compiles just fine. If you want to compile them individually, make sure FileOperation is first, because Summer depends on it.

Once it's compiled, you can run it with:

scala -cp .:chapter3:chapter4 chapter4.Summer
Sign up to request clarification or add additional context in comments.

5 Comments

It compiled with scalac chapter3/FileOperation.scala chapter4/Summer.scala However when I try to run, I get following error: >scala chapter4/Summer No such file or class on classpath: chapter4/Summer
@user1247412 You need to have a concept of java classpath in order to run Scala program in terminal using scala command directly. Otherwise you will have to use building tools(Eclipse / SBT...) that have already setup classpath for you.
This will run your program: scala -cp .:chapter3:chapter4 chapter4.Summer
Isn't there any simple way to compile and run the files from different packages?
Think of it in terms of 1) compiling everything that you pass to the compiler, 2) then putting everything that resulted on your classpath, and 3) running a single object that has a main() method. Your example above already uses 2 packages.

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.