JavaFX and Gradle

Exploring Gradle and tried to use it with JavaFX8. JavaFX is becoming the NEXT UI to use with Java.

Compiled a demo and built and compiled it normally. Runs fine.
Built directory structure, ran gradle:

C:\Code\DirectDrawDemo>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Error: Could not find or load main class org.gradle.DirectDrawDemo.Main
:run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:run’.

Process ‘command ‘C:\Java\jdk1.8.0_40\bin\java.exe’’ finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.367 secs

Okay so I know that JavaFX doesn’t have a main, it uses Application, so I changed build.gradle to look for Application and ran it again.

C:\Code\DirectDrawDemo>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Error: Could not find or load main class org.gradle.DirectDrawDemo.Application
:run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:run’.

Process ‘command ‘C:\Java\jdk1.8.0_40\bin\java.exe’’ finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 5.148 secs

Both failed. Currently reading up on JavaFX and the Application class. Any insights would be welcome. Any thoughts on a way to attack this would be welcome.

1 Like

C:\Code\DirectDrawDemo>gradle -v


Gradle 2.3

Build time: 2015-02-16 05:09:33 UTC
Build number: none
Revision: 586be72bf6e3df1ee7676d1f2a3afd9157341274

Groovy: 2.3.9
Ant: Apache Ant™ version 1.9.3 compiled on December 23 2013
JVM: 1.8.0_40 (Oracle Corporation 25.40-b25)
OS: Windows 7 6.1 x86

How are you configuring the ‘run’ task?

I actually hadn’t done any configuring to the run task yet. I had gone back to reading the Gradle User’s Guide to see what I can customize.

Normally I code in the NetBeans IDE so I was investigating changing Gradle to use the netBeans directory structure OR changing NetBeans to use Gradle’s directory structure.

Can you confirm that Gradle is actually compiling your source. By default, the Java plugin looks for code in ‘src/main/java’.

Agreed.
I understand that it looks in ‘src/main/java’. I verified that the directory tree exists as: Directory of C:\Code\DirectDrawDemo\src\main\java\org\gradle\DirectDrawDemo

04/06/2015 09:13 AM .
04/06/2015 09:13 AM …
03/30/2015 01:50 PM 2,494 DirectDrawDemo.java

I also changed DirectDrawDemo.java to Main.java in the same location. I believe that it really has to do with the fact that JavaFX extends Application rather than main. I don’t believe that Gradle can use it (as-is)

// Demonstrates drawing in JavaFX.

import javafx.application.;
import javafx.scene.
;
import javafx.stage.;
import javafx.scene.layout.
;
import javafx.scene.control.;
import javafx.event.
;
import javafx.geometry.;
import javafx.scene.shape.
;
import javafx.scene.canvas.;
import javafx.scene.paint.
;
import javafx.scene.text.*;

public class DirectDrawDemo extends Application {
GraphicsContext gc;
Color[] colors = { Color.RED, Color.BLUE, Color.GREEN, Color.BLACK };
int colorIdx = 0;

public static void main(String[] args) {
	launch(args);
}

//Override the start() method.
public void start(Stage myStage) {
		//Give the stage a title
		myStage.setTitle("Draw Directly to a Canvas");
		// Use a FlowPane for the root node.
		FlowPane rootNode = new FlowPane();
		//Center the nodes in the scene
		rootNode.setAlignment(Pos.CENTER);
		//Create a scene
		Scene myScene = new Scene(rootNode, 450, 450);
		// Set the Scene on the Stage.
		myStage.setScene(myScene);
		// Create a Canvas.
		Canvas myCanvas = new Canvas(400,400);
		// Get the graphic context for the Canvas.
		gc =myCanvas.getGraphicsContext2D();
		//Create a push button.
		Button btnChangeColor = new Button("Change Color");
			//Handle the action events for the change color button.
			btnChangeColor.setOnAction(new EventHandler<ActionEvent>()  {
				public void handle(ActionEvent ae){
					
					// Set the Stroke and fill color.
					gc.setStroke(colors[colorIdx]);
					gc.setFill(colors[colorIdx]);
					
					// Redraw the line, text and filled rectangle in the new color
					// This leaves the color of the other nodes unchanged.
					gc.strokeLine(0, 0, 200, 200);
					gc.fillText("This is drawn on the canvas.", 60 ,50);
					gc.fillRect(100,320,300,40);
					// Change the color
					colorIdx++;
					if(colorIdx == colors.length) colorIdx= 0;
				}
			});
			// Draw the intial output on the canvas.
			gc.strokeLine(0,0,200,200);
			gc.strokeOval(100,100,200,200);
			gc.strokeRect(0,200,50,200);
			gc.fillOval(0,0,20,20);
			gc.fillRect(100,320,300,40);
			
			// Set the font size to 20 and draw text
			gc.setFont(new Font(20));
			gc.fillText("This is drawn on the canvas.", 60, 50);
			
			// Add the canvas and button to the scene graph.
			rootNode.getChildren().addAll(myCanvas, btnChangeColor);
			
			//Show the stage and its scene.
			myStage.show();
}}

Gradle doesn’t care that it is a JavaFX class or what class it extends, only that it has a main() method.

when I run gradle check I get:
Executing command: “:check”
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE

BUILD SUCCESSFUL

Total time: 5.882 secs

Completed Successfully

Cool Thanks for clearing that up.

C:\Code\DirectDrawDemo>gradle run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Error: Could not find or load main class org.gradle.DirectDrawDemo.Main
:run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:run’.

Process ‘command ‘C:\Java\jdk1.8.0_40\bin\java.exe’’ finished with non-zero exit value 1

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.86 secs

Looking at the code example you posted I believe mainClassName should be set to org.gradle.DirectDrawDemo, since that is the class with the main() method. Also, not sure if it just got omitted during copy/paste but the DirectDrawDemo class doesn’t seem to have a package declaration. You might also try running with --stacktrace to see if we get any more useful information from the error.

DirectDrawDemo>gradle run --stacktrace
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run
Error: Could not find or load main class org.gradle.DirectDrawDemo.Main
:run FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:run’.

Process ‘command ‘C:\Java\jdk1.8.0_40\bin\java.exe’’ finished with non-zero exit value 1

  • Try:
    Run with --info or --debug option to get more log output.

  • Exception is:
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:run’.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:306)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:29)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:68)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:80)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:33)
    at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:24)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:36)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:51)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:169)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
    Caused by: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Java\jdk1.8.0_40\bin\java.exe’’ finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:365)
    at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
    at org.gradle.api.tasks.JavaExec.exec(JavaExec.java:60)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:63)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:218)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:211)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:200)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:585)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:568)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    … 44 more

BUILD FAILED

Total time: 4.197 secs

Hello,

Well I copied the DirectDrawDemo into the correct directory structure (according to Gradle) and it works !
C:\Users\parisr\Documents\NetBeansProjects\DirectDrawDemo\src\main\java\org\gradle\DirectDrawDemo.java

I found out that NetBeans IDE, that I usually use has a different directory structure: C:\Users\parisr\Documents\NetBeansProjects\DirectDrawDemo\src\org\gradle\DirectDrawDemo.java

Is there a way to make Gradle use the NetBeans directory structure?

1 Like

Yes. You can simply configure the source set.

sourceSets {
    main {
        java.srcDirs = ['src']
    }
}
1 Like

So not to be obtuse but I can make a plugin called NetBeans by customizing the sourceset. Using your example as a guide. It would/should then act like the eclipse plugin but for NetBeans?

You can refactor any configuration you want into a plugin. If you have a number of projects that are structured the “NetBeans way” then that might make sense. The Eclipse plugin is a bit more than that, it actually generates Eclipse metadata based on how your project is configured. Your goal is kind of the opposite, you want to configure you project based on the way NetBeans expects it to look.