LoginSignup
19
20

More than 5 years have passed since last update.

Play Framework2.4 For Java 開発環境

Posted at

Play Framework2.4

Play Framework が必要となったため試しに使ってみました。
・Windows8
・Play Framework2.4
・Java8

インストール

typesafe-activatorをダウンロードする。
2015-09-27_170447.png

ダウンロードしたファイルを解凍する。

typesafe-activator-1.3.6-minimal.zip

以下のディレクトリに解凍したファイルを格納をしてpathを通す。
C:\Program Files (x86)\activator-1.3.6-minimal

コマンドプロンプロを起動してactivatorを起動する。

C:\>cd play
C:\play>activator
A Java JDK is not installed or can't be found.
Please go to
  http://www.oracle.com/technetwork/java/javase/downloads/index.html
and download a valid Java JDK and install before running Activator.
If you think this message is in error, please check
your environment variables to see if "java.exe" and "javac.exe" are
available via JAVA_HOME or PATH.

Javaが入っていないと怒られるので、Java SE Development Kit 8をダウンロードする。
ダウンロードしたらjdk-8u60-windows-x64.exeを実行してインストールする。
以下のディレクトリへインストールされるのでpathを通す。
C:\Program Files\Java\jdk1.8.0_60\bin

もう一度、activatorを起動!色々初期ダウンロードが走るのでしばし待ちます。

C:\play>activator
Getting org.fusesource.jansi jansi 1.11 ...
downloading https://repo1.maven.org/maven2/org/fusesource/jansi/jansi/1.11/jansi-1.11.jar ...
        [SUCCESSFUL ] org.fusesource.jansi#jansi;1.11!jansi.jar (875ms)
~省略~
 Did not detect an activator project in this directory.
 - activator
 Load an existing project (has to be executed from the project directory)
 or print this help message if no project is found
 Sub-commands
 - activator ui
 Open the project in the UI if executed from an existing project
 directory, otherwise open a project-creation UI.
 - activator new [project-name] [template-name]
 Create a new project, prompting for project-name if missing and helping you
 find a template if template-name is not provided.
 - activator list-templates
 Fetch the latest template list and print it to the console.
C:\play>

アプリケーション作成

コマンドプロンプトから activator new my-app play-java と実行して、アプリケーションを作成する。
activator new [プロジェクト名] [テンプレート名]
テンプレートにはplay-java(Play Javaテンプレート), play-scala(Play Scalaテンプレート)などがある。
引数なしで実行すると一覧が表示さて対話形式で作成できる。

C:\play>activator new my-app play-java
Fetching the latest list of templates...
OK, application "my-app" is being created using the "play-java" template.
To run "my-app" from the command line, "cd my-app" then:
C:\play\my-app/activator run
To run the test for "my-app" from the command line, "cd my-app" then:
C:\play\my-app/activator test
To run the Activator UI for "my-app" from the command line, "cd my-app" then:
C:\play\my-app/activator ui

構成はこんな感じです。

C:\play\my-app
│  .gitignore
│  activator
│  activator-launch-1.3.6.jar
│  activator.bat
│  build.sbt
│  LICENSE
│  README
│
├─app
│  ├─controllers
│  │      Application.java
│  │
│  └─views
│          index.scala.html
│          main.scala.html
│
├─conf
│      application.conf
│      logback.xml
│      routes
│
├─project
│      build.properties
│      plugins.sbt
│
├─public
│  ├─images
│  │      favicon.png
│  │
│  ├─javascripts
│  │      hello.js
│  │
│  └─stylesheets
│          main.css
│
└─test
        ApplicationTest.java
        IntegrationTest.java

アプリケーションを実行!初回実行時、色々ダウンロードが走るのでまた待ちます。。。

C:\play>cd my-app
C:\play\my-app>activator run
~省略~
[info] Done updating.
--- (Running the application, auto-reloading is enabled) ---
[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Ctrl+D to stop and go back to the console...)

Server started と表示されたら、ポート9000番で起動しているのでブラウザからアクセスする。
http://localhost:9000/

コンパイルが終わるまでしばらく待つと・・・。

[info] Compiling 6 Scala sources and 2 Java sources to C:\play\my-app\target\scala-2.11\classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.6. Compiling...
[info]   Compilation completed in 26.289 s
Warning: node.js detection failed, sbt will use the Rhino based Trireme JavaScript engine instead to run JavaScript assets compilation, whic
h in some cases may be orders of magnitude slower than using node.js.
[info] - play.api.Play - Application started (Dev)

Application startedとコンソールに出る。

ブラウザを見ると画面が表示されました。
2015-09-27_170929.png

起動確認が終わったので、Ctrl + D でアプリケーションを終了する。

Eclipseプロジェクトに変換して取り込む

使い慣れているEclipseでプログラムをいじれるようにactivator eclipseでEclipseプロジェクトに変換する。

C:\play\my-app>activator eclipse
[info] Loading project definition from C:\play\my-app\project
[info] Set current project to my-app (in build file:/C:/play/my-app/)
[error] Not a valid command: eclipse (similar: help, alias)
[error] Not a valid project ID: eclipse
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: eclipse (similar: deliver, licenses, clean)
[error] eclipse
[error]        ^

errorが出て変換出来ず。。。
色々試したところ、
C:\play\my-app\project\plugins.sbt
に以下を追加すると
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")
eclipseコマンドが使えるようになりました。
参考Plugin for sbt to create Eclipse project definitions

C:\play\my-app>activator eclipse
[info] Loading project definition from C:\play\my-app\project
[info] Updating {file:/C:/play/my-app/project/}my-app-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Set current project to my-app (in build file:/C:/play/my-app/)
[info] About to create Eclipse project files for your project(s).
[info] Updating {file:/C:/play/my-app/}root...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Successfully created Eclipse project files for project(s):
[info] my-app

2.4以前は追加しなくてもよかったらしいです。

Eclipseを起動してプロジェクトをインポートます。※インストールは割愛します。
私はEclipse4.4 Luna Pleiades All in Oneを使いました。

・[ファイル] - [インポート] メニューからインポートダイアログを表示
・[一般] - [既存プロジェクトをワークスペースへ]を選択して「次へ」
・ルートディレクトリの選択の「参照」からC:\play\my-appを選択して「完了」ボタンを押下

インポート完了

EclipseでHello Worldサンプルアプリケーションを作る

app.viewshello_world.scala.htmlを追加する。
メッセージを受け取って画面に表示するだけです。

hello_world.scala.html
@(msg: String)
<html class='no-js' lang='ja'>
<head>
  <title>Hello World</title>
  <link rel="stylesheet" media="screen" href="/assets/stylesheets/main.css">
  <link rel="shortcut icon" type="image/png" href="/assets/images/favicon.png">
</head>
<body>
<div id="content" class="wrapper doc">
  <article>
    <h1>@msg</h1>
  </article>
</div>
</body>
</html>

conf.routes/hello_worldで呼び出すように定義します。

routes
GET     /hello_world                controllers.HelloWorld.showHelloWorld()

app.controllersHelloWorld.javaを追加する。
呼び出されたらメッセージを設定して返します。

HelloWorld.java
package controllers;

import play.mvc.Controller;
import play.mvc.Result;
import views.html.hello_world;

public class HelloWorld extends Controller {

    public Result showHelloWorld() {
        String msg = "Hello Play Framework.";
        return ok(hello_world.render(msg));
    }
}

プログラムの準備ができたらrunで実行して、ブラウザから
http://localhost:9000/hello_world
にアクセスします。

>activator run
[info] Loading project definition from C:\tools\play\workspase\my-app\
[info] Set current project to my-app (in build file:/C:/tools/play/wor

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 1 Java source to C:\tools\play\workspase\my-app\target\scala-2.11\classes...
[info] - play.api.Play - Application started (Dev)

コンパイルされて画面に表示されたら成功です。
2015-09-27_180146.png

import views.html.hello_worldでエラー

HelloWorld.javaでscala.htmlが見つからずにエラーとなった場合は、
2015-09-27_182130.png

Eclipseのビルドパスに
/my-app/target/scala-2.11/classes
を追加すると解消されます。

activatorで文字が化ける

activatorで↑矢印キーでコマンド履歴を使おうとすると文字化けしてしまいます。

>activator
[my-app] $ 潯

.activatorフォルダは以下に、activatorconfig.txtで文字コードを指定すると解消されます。
.activatorC:\Users\hogehoge\.activatorにあると思います。

activatorconfig.txt
-Dinput.encoding=Cp1252

その他

Ebean、Herokuなど他にも書き留めておきたいものがるので時間を見つけて書いていきます。

19
20
0

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
19
20