Javafx Spring Boot-运行应用程序时出错

时间:2018-07-25 19:30:29

标签: java spring javafx

这是我第一次在Spring Boot中使用Javafx 运行我的应用程序时出现以下错误

@GetMapping
public ResponseEntity<dataTableDTO> getProject(HttpServletRequest request, @RequestParam(name="draw") int draw) throws Exception  {...}

主要阶级

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]

}

控制器类

@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);
}

}

pom.xml

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
}    

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

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

请问发生了什么。 为什么org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE:run无法执行?

Scene.fxml代码

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

使用NetBeans IDE运行应用程序

2 个答案:

答案 0 :(得分:1)

好了,知道为什么会发生错误。我没有在控制器类中包含@Component。我的错误。

@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
   }

}

`现在一切正常

答案 1 :(得分:0)

我注意到您尚未显示Scene.fxml文件(用于指定应用程序布局的文件)或项目的目录结构。

检查/ src / main / resources / fxml /目录中是否具有Scene.fxml文件。

我已经运行了您的代码,包括在我的计算机中使用https://start.spring.io/创建基础项目,并将其用作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>

并运行命令 mvn clean spring-boot:run ,它可以正常工作。

检查是否正确配置了JAVA_HOME环境变量。

遵循此链接,以对带有弹簧启动的javafx进行介绍: https://wimdeblauwe.wordpress.com/2017/09/18/using-spring-boot-with-javafx/

希望这会有所帮助。