run(実行)する : Scalaでコードを書く
この記事はScalaでコードを書く時の、セットアップや動かし方、テストなどをまとめた研修用資料です。
実行
実行します。
※ コマンドプロンプトから実行します。eclipseはエディタとして使用し、eclipseから実行することはありません。
コマンドプロンプトから実行
- コマンドプロンプロトを起動し、プロジェクトが存在するディレクトリまで移動します。
cd [プロジェクトのルートディレクトリ]
- activatorを起動します。
Windowsの場合
activator.bat
Macの場合
./activator
ここから一緒です。
- Mainクラスを実行します。
> run
下記のように表示されれば成功です。
[info] Running com.example.Hello
Hello, world!
[success] Total time: 0 s, completed 2015/02/22 14:16:00
>
- コードを変更して、再度実行してみましょう。
(before)
Hello.scala
package com.example
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
(after)
Hello.scala
package com.example
object Hello {
def main(args: Array[String]): Unit = {
println("Aloha, world!")
}
}
- Mainクラスを再度実行します。
> run
下記のように表示されれば成功です。
[info] Running com.example.Hello
Aloha, world!
[success] Total time: 0 s, completed 2015/02/22 14:18:00
>
- 変更する度に「run」コマンドを実行するのは面倒です。「~」をコマンドの頭に追加すると、変更を検知して実行してくれます。
> ~run
下記のように表示されます。
[info] Running com.example.Hello
Aloha, world!
[success] Total time: 0 s, completed 2015/02/22 14:19:00
1. Waiting for source changes... (press enter to interrupt)
- eclipse上でコードを変更して保存してみます。
Hello.scala
package com.example
object Hello {
def main(args: Array[String]): Unit = {
println("Bye, world!")
}
}
- 変更を検知して、再度「run」コマンドが実行されます。
[info] Compiling 1 Scala source to /Applications/eclipse/workspace/minimal-scala/target/scala-2.11/classes...
[info] Running com.example.Hello
Bye, world!
[success] Total time: 4 s, completed 2015/02/22 14:20:00
2. Waiting for source changes... (press enter to interrupt)