Getting a Scala interpreter to work

2020-02-17 06:40发布

I'm very new to Scala. I have downloaded it, got it working in Eclipse where I'll be developing it; but I can't make it work in Terminal.

All sites and books say to just type scala - this doesn't work.

The website infuriatingly says:

We assume that both the Scala software and the user environment are set up correctly.

How do I do that bit?

I'm very new to this, and using Jargon or assuming too much knowledge of frameworks around Scala will ruin a good response; please keep it simple.

  • Mac OS X (10.6.7)
  • Scala: 2.9.0.1

Thank you

7条回答
成全新的幸福
2楼-- · 2020-02-17 06:42

Scala recommends using Homebrew to install the Typesafe stack for Scala 2.9.2.

brew install scala sbt maven giter8

Homebrew will install soft links in /usr/local/bin for sbt, scala, scalac, scaladoc, scalap, fsc and g8. Follow the soft links to its final referent in order to determine where $SCALA_HOME needs to be. $SCALA_HOME should contain bin/scala and lib/scala-compiler.jar.

Typesafe recommends using sbt console instead of scala to get the interpreter going, because sbt will also manage library dependencies to libraries such as Akka. That said, if you want to use scala, scalac, fsc, scalac and scaladoc directly, you may need to run a chmod +x on the referents of the soft links.

查看更多
时光不老,我们不散
3楼-- · 2020-02-17 06:49

You need to add scala\bin to your PATH environment variable.

On Mac OS X the path to Scala bin is usually: /Users/<your username>/scala/bin and on Windows usually: C:\Program Files (x86)\scala\bin.

On Mac OS X use the Terminal and write (using your username):

echo 'export PATH=/Users/<your username>/scala/bin:$PATH' >> ~/.bash_profile

then close the Terminal and start it again.

查看更多
家丑人穷心不美
4楼-- · 2020-02-17 06:54

For OS X, I highly recommend Homebrew.

The installation of Homebrew is incredibly easy. Once installed, you just need to run brew install scala and scala will be installed and ready to go. Homebrew also has tons of other goodies just a brew install away.

If you don't already have Java installed, you can install that with brew cask install java.

MacPorts or Fink may have something similar, but I prefer Homebrew.

查看更多
男人必须洒脱
5楼-- · 2020-02-17 06:54

Not a big fan of cluttering up my PATH variable. I just symlink all my programs to /usr/local/bin, which is in the classpath. For example, if you downloaded scala and uncompress it in /opt/scala-2.9.0-1, run the following in the terminal.

ln -s /opt/scala-2.9.0-1/bin/scala /usr/local/bin

Now just type scala in the terminal and you're all set. This way you don't have to set your PATH or change it when you have a new version of scala you want to try out. If you download a new version, you can uncompress it in any location and symlink the new version. Say you download version 2.9.1 and uncompress it in /opt/scala-2.9.1. You can type the following in the terminal

ln -s /opt/scala-2.9.1/bin/scala /usr/local/bin/scala2.9.1

Now, to use scala 2.9.1 you just run scala2.9.1 at the terminal. When you are ready to switch to 2.9.1 fulltime, just update the symlink.

You could also add scaladoc, scalac, scalap and others in the same way

ln -s /opt/scala-2.9.0-1/bin/scalac /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/scalap /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/scaladoc /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/fsc /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/sbaz /usr/local/bin
ln -s /opt/scala-2.9.0-1/bin/sbaz-setup /usr/local/bin
查看更多
男人必须洒脱
6楼-- · 2020-02-17 06:57

If you downloaded scala with macports try typing scala-2.9 (or whatever is the filename of the contents of folder /opt/local/bin/scala/)

(At least this works for OSX mountain lion)

查看更多
欢心
7楼-- · 2020-02-17 07:04

My way of making Scala run every time you type "scala" in Terminal was to add the path not to the .bashrc file but to the /etc/paths one.

  1. Download the latest Scala binary from www.scala-lang.org/download
  2. Unzip the binary file
  3. Move the unzipped folder (named scala-2.11.2) to the /usr/bin/ or some other directory of your preference.
  4. Then open the /etc/paths file with nano:

    sudo nano /etc/paths
    
  5. and add this line to the end:

    /usr/bin/scala-2.11.2/bin
    
  6. Press Ctrl+X to exit nano and answer "Yes" when prompted to overwrite the file

  7. Then restart Terminal and once you type:

    scala
    

    you will get the scala command line that looks like this:

    Welcome to Scala version 2.11.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> 
    

I hope this helps.

查看更多
登录 后发表回答