It is possible to use JavaFX in Groovy using the same syntax as in Java?

groovy, javafx

Solution

replace the main method with:

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

The error basically gives you the answer: no you can not just copy the files in all cases. There are differences between groovy and java (e.g. http://groovy-lang.org/differences.html). Groovy comes with a tool called `java2groovy` that might help migrate.

But as groovy runs fine with java code, why bother? Migrate the parts, that are better off using groovy and keep the java parts around for now.

Problem

I am using jdk1.8.0_25. I am trying to run a JavaFX app file below which, when named 'HelloWorldMain.java', compiles and runs OK with javac/java. I renamed it as 'HelloWorldMain.groovy' and can't run it using Groovy. Is there a simple way to run this file using Groovy with no or minimal modification, preferably without additional software like GroovyFX? And if I have to use GroovyFX, can I run this pure Java code without modification? ``` import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; /** * * @author cdea */ public class HelloWorldMain extends Application { /** * @param args the command line arguments */ public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World"); Group root = new Group(); Scene scene = new Scene(root, 300, 250); Button btn = new Button(); btn.setLayoutX(100); btn.setLayoutY(80); btn.setText("Hello World"); btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent event) { System.out.println("Hello World"); } }); root.getChildren().add(btn); primaryStage.setScene(scene); primaryStage.show(); } } ``` I am trying to run it as groovy HelloWorldMain.groovy and getting the following output in command line: ``` Caught: java.lang.RuntimeException: java.lang.ClassNotFoundException: javafx.application.Application$launch java.lang.RuntimeException: java.lang.ClassNotFoundException: javafx.application.Application$launch at javafx.application.Application.launch(Application.java:260) at javafx.application.Application$launch.call(Unknown Source) at HelloWorldMain.main(HelloWorldMain.groovy:20) Caused by: java.lang.ClassNotFoundException: javafx.application.Application$launch at javafx.application.Application.launch(Application.java:248) ... 2 more ```

Original source