JavaFX中launch(String[])方法未定义报错的解决方法
校园播放列表GUI启动报错:The method launch(String[]) is undefined for the type application
我在开发一个校园播放列表GUI项目,队友引入Maven组件来实现音频上传、取值和播放功能。但导入队友的Maven相关文件后,GUI无法启动,报编译错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method launch(String[]) is undefined for the type application
at playlistProject/playlist.application.main(application.java:157)
原本预期GUI正常弹出,附上代码以排查问题是代码、JavaFX还是Maven配置导致的:
package playlist; import java.io.File; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.collections.ObservableList; import javafx.collections.FXCollections; import javafx.scene.control.cell.ComboBoxListCell; import javafx.scene.layout.StackPane; import javafx.scene.control.Button; import playlist.*; import playlist.Playlist; import javafx.stage.FileChooser; import javafx.scene.control.ListView; public class application { private ListView<String> allSongs = new ListView<>(); //public ObservableList test = FXCollections.observableArrayList(); private TextField txtSongName = new TextField(); private TextField txtSongArtist = new TextField(); private TextField txtGenre = new TextField(); public void start(Stage primaryStage) { try { GridPane root = new GridPane(); root.setPadding(new Insets(20)); root.setHgap(15); root.setVgap(15); Scene scene = new Scene(root,700,600); root.add(search(), 0, 0); root.add(searchBoxes(), 0, 1); FileChooser fileChooser = new FileChooser(); Button button = new Button("Click to add song files"); EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { File file = fileChooser.showOpenDialog(primaryStage); } }; button.setOnAction(event); root.add(button, 0, 2); root.add(listViewSongs(), 0, 3); root.add(additionalButtons(), 0, 4); listViewSongs(); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setScene(scene); primaryStage.setTitle("Playlist"); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } private Pane search() { HBox row = new HBox(); Label label = new Label("Search song by:"); row.getChildren().add(label); return row; } private Pane searchBoxes() { HBox row = new HBox(); Label song = new Label("Song Name: "); txtSongName.setPrefWidth(150); Label artist = new Label(" Artist: "); txtSongArtist.setPrefWidth(150); Label genre = new Label(" Genre: "); txtGenre.setPrefWidth(150); row.getChildren().addAll(song, txtSongName, artist, txtSongArtist, genre, txtGenre); return row; } private Pane listViewSongs() { VBox column = new VBox(); Label label = new Label("Playlist"); allSongs.setPrefHeight(100); column.getChildren().addAll(label, allSongs); return column; } private Pane additionalButtons() { GridPane grid = new GridPane(); grid.setHgap(10); grid.setVgap(10); Button btnPlayPause = new Button("Play/Pause"); btnPlayPause.setOnAction(new handlePlayPause()); Button btnSkip = new Button("Skip"); btnSkip.setOnAction(new handleSkip()); Button btnShuffle = new Button("Shuffle"); btnShuffle.setOnAction(new handleShuffle()); Button btnRemove = new Button("Remove Song"); btnRemove.setOnAction(new handleRemove()); grid.add(btnPlayPause, 0, 4); grid.add(btnSkip, 1, 4); grid.add(btnShuffle, 2, 4); grid.add(btnRemove, 3, 4); return grid; } public class handlePlayPause implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub } } public class handleSkip implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub } } public class handleShuffle implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub } } public class handleRemove implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent arg0) { // TODO Auto-generated method stub } } public static void main(String[] args) { launch(args); } }
问题排查与修复方案
1. 核心代码问题
报错直接原因是主类未继承JavaFX的Application基类。launch()是Application类的静态方法,只有继承该类才能直接调用。
- 修改类定义:
(规范建议类名首字母大写,可改为public class application extends Application {PlaylistApplication避免和JavaFX基类重名)
2. Maven配置验证
确保pom.xml中正确引入JavaFX依赖,示例配置(根据使用的JavaFX版本调整):
<dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>21.0.1</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-media</artifactId> <version>21.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.openjfx</groupId> <artifactId>javafx-maven-plugin</artifactId> <version>0.0.8</version> <configuration> <mainClass>playlist.application</mainClass> </configuration> </plugin> </plugins> </build>
- 注意
mainClass要对应主类的全路径。
3. 启动方式验证
- IDE启动:选择JavaFX应用类型的运行配置,而非普通Java应用。
- Maven命令启动:执行
mvn javafx:run。
额外优化
- 移除冗余导入:比如
playlist.*与playlist.Playlist重复、未使用的StackPane导入。
内容的提问来源于stack exchange,提问作者Aspen
相关产品推荐
相关产品推荐

