0

I am reading Martin Odersky's Programming in Scala, and I have been using vi and the command line to compile so far. I want to learn to use Eclipse and the Scala-IDE plug-in, but I lack a basic understanding of compiling and executing multiple source code files in Eclipse.

My IDE is as follows:

  • Ubuntu 12.04
  • Eclipse 3.7.2
  • Scala Plugin 2.0.1
  • Scala Library 2.9.2

I am using the Checksum example in Chapter 4 for practice. The source code is as follows:

Listings 4.1 & 4.2 / ChecksumAccumulator.scala:

class ChecksumAccumulator {
    private var sum = 0
    def add(b: Byte) { sum += b }
    def checksum(): Int = ~(sum & 0xFF) + 1
}

import scala.collection.mutable.Map

object ChecksumAccumulator {

    private val cache = Map[String, Int]()

    def calculate(s: String): Int = 
        if (cache.contains(s))
            cache(s)
        else {
            val acc = new ChecksumAccumulator
            for (c <- s)
                acc.add(c.toByte)
            val cs = acc.checksum()
            cache += (s -> cs)
            cs
        }
}  

Listing 4.3 / Summer.scala:

import ChecksumAccumulator.calculate

object Summer {
    def main(args: Array[String]) {
        for (arg <- args)
            println(arg + ": " + calculate(arg))
    }
}

I can compile these classes using scalac Summer.scala ChecksumAccumulator.scala at the command line. I can then execute the object code with the command line scala Summer of love, which returns the checksum results for "of" and "love" (-213 and -182, respectively).

How would I build the object code using Eclipse and not the command line, and how would I call the Summer object code through Eclipse?

David Kaczynski
  • 1,246
  • 2
  • 20
  • 36
  • I am not familiar with Scala. But according to my understating , You have to create a Scala Project in eclipse include your Source file in source directory. After that Right click on source file then select run . http://scala-ide.org/docs/tutorials/lift24scalaide20/index.html – nayakam Jun 18 '12 at 05:40
  • Create a scala project and switch to the scala perspective,put both file in some package of the project.Run scala with Ctrl+F11 and choose to run as `scala application`.BTW,if you're not familiar with java I suggest you should not learn scala at first. – Hongxu Chen Jun 18 '12 at 07:11
  • I'd highly recommend to watch the following screencasts: [Getting Started with the Scala IDE](http://scala-ide.org/download/current.html) and [Scala IDE Features Overview](http://scala-ide.org/). In about 15 minutes you will be a Scala IDE pro ;-) – Mirco Dotta Jun 18 '12 at 08:51
  • Thank you all very much. I plan on going through each of these methods after work today. Hongxu, I am familiar with Java, but I only know how to build and deploy .war files with Maven in Eclipse. In school, I used JGrasp, which automatically associated all open source code files into the same compilation. Once I finish the book, I plan on using Maven and the Lift framework, but I want to learn the fundamentals first. – David Kaczynski Jun 18 '12 at 12:33

1 Answers1

1

In Eclipse, you can just create a new Scala project, and put the source files there. Once the files are part of the same project, there's no need to do anything special:

Just click on Run >> Run As >> Scala Application (or press Alt + Shift + X, S)

Have fun learning Scala :)

Vinicius Seufitele
  • 885
  • 1
  • 6
  • 15
  • Thank you very much. I was getting confused because `Scala Application` was not an option from the `Run -> Run as ->` menu, but using the shortcut worked. This gave me a baseline run configuration to which I successfully added command line arguments through the `Run Configurations` interface. So cool, thanks again! – David Kaczynski Jun 18 '12 at 23:58