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 [twitter:@sk44_] 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'
apply from: 'http://dl.bintray.com/content/shemnon/javafx-gradle/0.3.0/javafx.plugin'
EDIT: 2013-07-27

I've edited the javafx-gradle's URL.

It seems that the URL is often changed. Please be sure to check the latest information in developer's blog etc.

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...

EDIT: 2013-07-27

This error was already fixed in javafx-gradle 0.3.0. I've tried it with NetBeans 7.3.1.

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((You can also copy & paste from NetBeans' JavaFX project template generated code. Never forget to call launch.)), 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!