LoginSignup
3
5

More than 5 years have passed since last update.

【初心者向け】Scalaを動かす

Posted at

Java文化を知らずにScalaを動かす

特にLL言語から入ったり等の理由でJava文化圏にノータッチだった人が、最近はScalaに興味を持つことも多いと思います。というか俺得な記事です。

  • Java1.6か1.7が動く環境を作りましょう。java コマンドにpathが通ってるように。
  • Scalaが動く環境にはscala, sbt/Play!などがあります。これらはzipかtgzで拾ってきてスクリプト(Windowsだとバッチファイル)にpathを通す事で動作します
  • sbt/Play!には、モダンなパッケージ管理システムが組み込まれてます。基本的にはsbt/playを使いましょう

scala実行環境

例 (scala-2.10.2をtgzで/usr/local/scalaにインストールする場合)

$ cd /usr/local
$ tar zxvf scala-2.10.2.tgz
$ ln -s scala-2.10.2 scala

後は/usr/local/scala/binにpathを通せば、scalaコマンドが実行できます。

実行

$ scala
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> 

こんな感じのいわゆるREPL画面が起動するので、対話型でScalaのコードを実験できます。また scala hoge.scala のように、インタプリタっぽく起動する事も可能です。ただし、素のscalaコマンドはライブラリ管理も何もしてくれないのできわめて扱いづらいです。

sbt実行環境

sbtはライブラリ管理とビルドを司る環境です。

例 (sbt.tgzを/usr/local/sbtにインストールする場合)

$ cd /usr/local
$ tar zxvf sbt.tgz

後は/usr/local/sbt/bin/にpathを通せばsbtコマンドが実行できます

実行

$ sbt
Getting net.java.dev.jna jna 3.2.3 ...
〜〜〜中略〜〜〜
[]info] Set current project to default-xxxxxx (in build file:xxxxxxxx)
> 

sbtのプロンプトで、console と打つと、様々なライブラリを読み込んだ状態で Scala REPL 環境が起動します。

sbtは設定ファイルにより、様々なライブラリ解決やビルド定義ができますが、それはまた別の記事にて。

Play!実行環境

Play!(Play framework)は、sbtベースのウェブ向け統合環境(Railsみたいなもの)です。Play!はzipのみでしか配布していない為、パーミッション周りが微妙に面倒な事になっています。

例 (play-2.1.2.zipを/usr/local/playにインストールする場合)

$ cd /usr/local
$ unzip play-2.1.2.zip
$ ln -s play-2.1.2 play
$ mkdir play/framework/sbt/boot
$ chmod go+w play/framework/sbt/boot
$ chmod -R go+w play/repository/

後は/usr/local/play(binではない)にpathを通せばplayコマンドを実行できます。

Play frameworkインストール先に書き込み権限が必要なのでユーザーのディレクトリ下にインストールした方がいいかもしれません。

実行

$ play
Getting net.java.dev.jna jna 3.2.3 ...
中略
play! 2.1.2 (using Java 1.6.0_24 and Scala 2.10.0), http://www.playframework.org

This is not a play application!

Use `play new` to create a new Play application in the current directory,
or go to an existing application and launch the development console using `play`.

You can also browse the complete documentation at http://www.playframework.org.

play とだけ叩くと、今いるディレクトリはPlayアプリじゃないと言われます。

$ play new test
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.1.2 (using Java 1.6.0_24 and Scala 2.10.0), http://www.playframework.org

The new application will be created in /Users/erukiti/test

What is the application name? [test]
> 

Which template do you want to use for this new application? 

  1             - Create a simple Scala application
  2             - Create a simple Java application

> 1
OK, application test is created.

Have fun!

まずは play new <アプリ名> でplayアプリケーションディレクトリを作成しましょう。

$ cd test
$ play
[info] Loading project definition from /xxxxxx/test/project
[info] Set current project to test (in build file:/xxxxxx/test/)
       _            _
 _ __ | | __ _ _  _| |
| '_ \| |/ _' | || |_|
|  __/|_|\____|\__ (_)
|_|            |__/

play! 2.1.2 (using Java 1.6.0_24 and Scala 2.10.0), http://www.playframework.org

> Type "help play" or "license" for more information.
> Type "exit" or use Ctrl+D to leave this console.

[test] $ 

playディレクトリでplayコマンドを実行するとplayコマンドプロンプトが立ち上がります。ここで、sbt同様にplay consoleを実行すると、playの様々なライブラリが読み込まれた状態でScala REPL環境が立ち上がります。

3
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5