使用 'mvn spring-boot:run' 启动 JavaFX Spring 项目
Start JavaFX Spring project with 'mvn spring-boot:run'
如何使用扩展 javafx.application.Application
且不包含 main 方法的 Maven 就地启动 spring-boot 项目?每次我尝试使用 Maven 启动它时,都会显示以下错误。
错误信息:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.5.RELEASE:run (default-cli) on project chatClient: An exception occurred while running. The specified mainClass doesn't contain a main method with appropriate signature.: 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。
Here 你有一个完整的例子。
更新
现在我注意到您没有 main 方法。将此代码添加到 ChatClient
class:
public static void main(String[] args) {
Application.launch(ChatClient.class, args);
}
如何使用扩展 javafx.application.Application
且不包含 main 方法的 Maven 就地启动 spring-boot 项目?每次我尝试使用 Maven 启动它时,都会显示以下错误。
错误信息:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.5.RELEASE:run (default-cli) on project chatClient: An exception occurred while running. The specified mainClass doesn't contain a main method with appropriate signature.: 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。
Here 你有一个完整的例子。
更新
现在我注意到您没有 main 方法。将此代码添加到 ChatClient
class:
public static void main(String[] args) {
Application.launch(ChatClient.class, args);
}