1

This is my first time using Javafx with Spring Boot I'm have the below error when running my Application

Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run (default-cli) on project basic: Could not exec java: Application finished with exit code: 1 -> [Help 1]

The Main Class

@SpringBootApplication
public class BasicApplication extends Application {

private Parent root;
private ConfigurableApplicationContext context;

@Override
public void init() throws Exception {
    context = SpringApplication.run(BasicApplication.class);
    FXMLLoader loader=new FXMLLoader(getClass().getResource("/fxml/Scene.fxml"));
    loader.setControllerFactory(context::getBean);
    root=loader.load();
}

@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}

@Override
public void stop() throws Exception {
    context.close();
}

public static void main(String[] args) {
    launch(BasicApplication.class,args);
}

}

Controller Class

public class SceneController implements Initializable {

@FXML
private Label label;

@FXML
private void buttonAction(){
    label.setText("Hello World!");
} 

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.example</groupId>
<artifactId>basic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>basic</name>
<description>Basic project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

Please what is going on. Why org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run can't be executed?

Scene.fxml code

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="com.example.SceneController">
<children>
    <Button layoutX="126" layoutY="90" onAction="#buttonAction" text="Click Me!" />
    <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
</children>

am running the application with NetBeans IDE

2
  • how are you running the project? are you running a command like mvn clean spring-boot:run ? Could you show the project structure? Commented Jul 25, 2018 at 19:36
  • no. am running it with netbeans Commented Jul 25, 2018 at 20:30

2 Answers 2

1

Ok have seen why the error occur. I didn't include @Component in the Controller Class. my Mistake.

@Component
public class SceneController implements Initializable {
   @FXML
   private Label label;

   @FXML
   private void buttonAction(){
        label.setText("Hello World!");
   }

   @Override
   public void initialize(URL url, ResourceBundle rb) {
         // TODO
   }

}

`Now Everything is working fine

Sign up to request clarification or add additional context in comments.

Comments

0

I've noticed you have not shown your Scene.fxml file (the one that specifies the layout of your app) or your project's directory structure.

Check that you have a Scene.fxml file in the /src/main/resources/fxml/ directory.

I've run your code including in my machine creating a base project with https://start.spring.io/ , using this as Scene.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx">
    <Label text="Main Content"/>
</AnchorPane>

and running the command mvn clean spring-boot:run and it works fine.

Check if you have the JAVA_HOME environment variable correctly configured.

follow this link for an introduction into javafx with spring boot: https://wimdeblauwe.wordpress.com/2017/09/18/using-spring-boot-with-javafx/

Hope this helps.

1 Comment

added the Scene.fxml (see edit). am actually using netbeans to run the project. thanks for your response i will go thru that link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.