Hatena::ブログ(Diary)

hd 4.0 RSSフィード

2013-02-08

Start developing JavaFX application with NetBeans and Gradle

Introduction

I want to create JavaFX application with NetBeans and Gradle. This post is my memo to start developing JavaFX application with NetBeans and build with Gradle. If you find something wrong in this post, please tell me by comment or mention to @ on Twitter.

Sorry for my poor English...

この記事は NetBeans + Gradle で JavaFX 開発を始めてみる - hd 4.0 の英訳版です。日本語でおkな方は元記事をご参照ください。誰か英語添削してほしい(´・ω・`)

Environment

Gradle installation

  1. Google it, download and install it :-)
  2. Add GRADLE_INSTALLATION_DIR\bin to path in environment variable in windows.

NetBeans Gradle support plugin installation

  1. From the Tool menu, choose Plugin and open Available Plugins.
  2. In the search text box, type "gradle", you will find it in plugins list and install it.
  3. From the Tool menu, choose Option and open other -> Gradle.
  4. Set GRADLE_INSTALLATION_DIR to Gradle Installation Directory.

This time I don't refer, if you want to use GroovyFX, you shoud install Groovy plugin. This post is very useful for GroovyFX(in Japanese).

Creating new project by NetBeans

  1. From the File menu, choose New Project.
  2. In the Gradle category, choose Single Gradle Project and create it.
    • if you not set the Main Class, then NetBeans will raise error later... You should set something to it.

Editing build.gradle

To build JavaFX application using gradle, use javafx-gradle. This provides Gradle build tasks for JavaFX.

To use the javafx-gradle plugin, include this in your build.gradle.

apply from: 'http://dl.bintray.com/content/shemnon/javafx-gradle/javafx.plugin'

Cofigure application main class like this.

// This is a default main class configuration generated by NetBeans.
// This configuration is not for javafx-gradle, so remove this.
//if (!hasProperty('mainClass')) {
//    ext.mainClass = 'foo.Main'
//}

// Configure like this.
javafx {
    mainClass = 'foo.Main'
}

Then, from NetBeans, choose Run and you will see "BUILD SUCCESSFUL" in the Output window.

You can also run gradle build from Windows Command prompt. But from NetBeans, choose Build and you will get following error(sorry in Japanese).

Build failure: gradle build
org.gradle.tooling.BuildException: Could not execute build using Gradle installation 'D:\opt\gradle\gradle-1.4'.
	at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:53)
	at org.gradle.tooling.internal.consumer.async.DefaultAsyncConnection$3.run(DefaultAsyncConnection.java:81)
	at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.gradle.api.internal.LocationAwareException: Execution failed for task ':jfxJar'.
(略)
Caused by: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':jfxJar'.
(略)
Caused by: com.sun.javafx.tools.packager.PackagerException: エラー: jarファイルJavaFxGradle.jarの作成に失敗しました
(略)
Caused by: java.io.FileNotFoundException: Input folder does not exist [C:\Program Files\NetBeans 7.2.1\build\jdk7Compat\JavaFxGradle.jar]

I've not solved this problem yet...

Implement main class

At this time, your main class is not a JavaFX entry point, therefore nothing happen after Run. You have to implement it. JavaFX application entry point class should extends javafx.application.Application.

If you cannot find javafx.application package from NetBeans' proposal, then right-click on the project and choose Reload Project. You will find jfxrt.jar in Dependencies/Compile folder in your project.

After you implement main class*1, run application, you will see JavaFX application's window!

After that you do, you can write code whatever you want to. You can also use FXML without problem.

Well, if you use NetBeans' JavaFX template without using Gradle, you will not take more than a minute to run application... But I want to use Gradle!

*1:You can also copy & paste from NetBeans' JavaFX project template generated code. Never forget to call launch.

2013-02-06

NetBeans + Gradle で JavaFX 開発を始めてみる

今日は大雪だというので多分電車も止まるだろうとばっちり休むつもりでいたら、たいして雪もふらず電車も止まってなかったけど休んだ。

というわけで暇だったので NetBeans + Gradle で JavaFX 開発でもやってみようとあれこれ試行錯誤したメモ。とりあえず画面出すまで。変なとこあったらぜひ教えていただきたい。

環境

Gradle インストール

gradle でぐぐって出てきたやつを入れる。インストールフォルダ\bin を環境変数 path に追加しておく。

NetBeansプラグイン追加

NetBeans の Gradle サポートを入れる。 NB のツール -> プラグイン -> 使用可能なプラグインから、検索で gradle と入れて出てきた奴をインストールすればいい。

ツール -> オプション -> その他 -> Gradle の Gradle Installation Directory に Gradle のインストールフォルダを設定する。これは bin は含めなくていい。

今回は触れないけど、 GroovyFX にするなら Groovy プラグインを入れておくといいと思う。 GroovyFX については以下の記事が詳しい。

プロジェクト作成

NB の新規プロジェクトから Gradle -> Single Gradle Project を選択して作成。ここで Main Class を指定しないと NB が例外ふいたので適当に指定しておく。

build.gradle の変更

gradle で JavaFX アプリケーションビルドするために、 javafx-gradle を使う。

build.gradle に以下を追記して読み込む。

apply from: 'http://dl.bintray.com/content/shemnon/javafx-gradle/javafx.plugin'

続いて、Main Class の指定方法を以下の通り変更。

// NB が生成したひな形に以下のような指定があるが、これを消して、
//if (!hasProperty('mainClass')) {
//    ext.mainClass = 'foo.Main'
//}

// こんな感じにする
javafx {
    mainClass = 'foo.Main'
}

これで NB から実行( Run )するとめでたく BUILD SUCCESSFUL となる。

この状態でコマンドプロンプトから gradle build するとビルド通るんだけど、 NB から Build するとエラーになる。

Build failure: gradle build
org.gradle.tooling.BuildException: Could not execute build using Gradle installation 'D:\opt\gradle\gradle-1.4'.
	at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:53)
	at org.gradle.tooling.internal.consumer.async.DefaultAsyncConnection$3.run(DefaultAsyncConnection.java:81)
	at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:722)
Caused by: org.gradle.api.internal.LocationAwareException: Execution failed for task ':jfxJar'.
(略)
Caused by: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':jfxJar'.
(略)
Caused by: com.sun.javafx.tools.packager.PackagerException: エラー: jarファイルJavaFxGradle.jarの作成に失敗しました
(略)
Caused by: java.io.FileNotFoundException: Input folder does not exist [C:\Program Files\NetBeans 7.2.1\build\jdk7Compat\JavaFxGradle.jar]

これはよくわからない。

Main の変更

このままでは Main クラスが JavaFX でもなんでもないただの Main クラスなので当然 Run しても何も起きない。 JavaFX のエントリポイントになるように Main を実装する必要がある。 JavaFX のエントリポイントといえば javafx.application.Application ですね。

というわけでおもむろに Main クラスに extends Application しようとしてもなぜか import が見つからない。そんな時は慌てずに落ち着いてプロジェクト右クリック -> Reload Project すればいい。 Dependencies/Compile の下に jfxrt.jar が見えていれば大丈夫。

あとは Application の抽象メソッドを実装して*1 Run すればめでたく JavaFX アプリケーションが起動してくるはず。

あとは普通に開発していけばいい。 FXML とかも問題なく使える。わざわざこんなことせずに NetBeansJavaFX テンプレート使えば画面出すのなんて 1 分かからないけどな・・・。

Scene Builder について

JavaFX の画面レイアウト( FXML )を編集するための Scene Builder 、初めて使ってみた。 NetBeans 入れたら勝手に入ってくるんだとなぜか思い込んでたけど、別途インストールが必要なのね。しかも JDK だけ入れて JRE 入れてなかったらインストールすら出来なかった。

FXML を手で編集するとか苦行すぎると思うのでこのツールは助かる。ただなんか onAction の設定あたりの動きが怪しい気がするなあ。メソッド名が途中までしか FXML に反映されないとか。まだまだ発展途上ということか。

難しいかもしれんけど早く NetBeans に統合されるといいのにな。 IDEGUI 編集ツールが含まれてないってよく考えたらかなり異次元よね。

Scene BuilderUI 綺麗だけど JavaFX で作られてるのかな。 NetBeansJavaFX 化するのは何時の日か・・。

余談

NetBeans って Eclipse と違ってソースフォルダ( src/main/java )のパスが表示されないのな。 GroovyFX もちょっとためそうと思ったんだけど、この src/main/java のパスを変更する方法がわからなかった*2。めんどくさくなったので、 NetBeans 外でフォルダ名を java -> groovy にして Reload Project したら変更することができた。

@mike_neck さんによるとこういう方法もあるようだ。

なお、 src/main/javasrc/main/groovy を両方用意しとくと、 NetBeans からは Source Packages が2つ見えるようになり、中身が空だとどっちがどっちかわからなくなるw

*1:面倒だったら JavaFX プロジェクトのテンプレートからコピペしてきてもいい。 launch の呼び出しを忘れずに。

*2:Main クラスが Groovy の場合、 src/main/groovy に置かないと認識されない。