使用 'mvn spring-boot:run' 启动 JavaFX Spring 项目



如何使用maven来启动一个spring-boot项目,该项目扩展了不包含主方法的javafx.application.Application?每次我尝试用maven启动它时,都会显示下面的错误。

错误消息:

[错误]无法执行目标org.springframework.boot:spring-boot maven插件:2.0.5。发布:在项目聊天客户端上运行(默认cli(:运行时发生异常。指定的mainClass不包含具有适当签名的main方法。:XX、 XXX.ChatClient.main([Ljava.lang.String;(->[Help 1]

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>XX.XXX</groupId>
<artifactId>chatClient</artifactId>
<version>1.1.4</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<start-class>XX.XXX.ChatClient</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

ChatClient.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import java.io.IOException;
@SpringBootApplication
public class ChatClient extends Application {
private ConfigurableApplicationContext springContext;
private Parent root;
@Override
public void init() throws IOException {
springContext = SpringApplication.run(ChatClient.class);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/root.fxml"));
fxmlLoader.setControllerFactory(springContext::getBean);
root = fxmlLoader.load();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Chat client");
primaryStage.centerOnScreen();
primaryStage.show();
}
@Override
public void stop() {
springContext.stop();
}
}

root.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane></BorderPane>

替换:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>

带有:

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

不需要指定mainClass。

这里有一个完整的例子。

更新

现在我注意到你没有一个主要的方法。将此代码添加到ChatClient类:

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

相关内容

  • 没有找到相关文章

最新更新