用Idea寫了一個JavaFX項目,創建artifacts,build artifacts,運行build出來的exe可執行文件時總是遇到 class not found的錯誤,如下圖
一開始根據提示以為是class路徑沒設置好,但怎么改都是一樣的錯誤,網上也搜不到跟我遇到同樣問題的人。
后來新建了個JavaFX項目,不用Maven,發現可以正常部署,可以找到Main類。
最后兩個項目之間一點點地修改,一點點地比對,發現問題出在靜態屬性的定義並初始化上,比如下面代碼中有四個靜態屬性並且都在類加載時進行初始化
控制變量法式地測試發現,單純定義了靜態屬性的話,沒有問題可以找到Main類,但如果在定義語句后面初始化賦值,就會導致上面圖中的class not found的錯誤
public class Main extends Application { private Logger logger = LoggerFactory.getLogger(Main.class); private Stage primaryStage; private BorderPane rootLayout; private SoundService soundService = new SoundService(); private static Image fileIcon = new Image(Main.class.getResource("/images/iconfinder_icon-55-document-text_315178.png").toString(), 16, 16, true, true); private static Image folderIcon = new Image(Main.class.getResource("/images/iconfinder_icon-94-folder_314778.png").toString(), 16, 16, true, true); private static Image musicIcon = new Image(Main.class.getResource("/images/iconfinder_icon-63-document-music_315187.png").toString(), 16, 16, true, true); private static Image audioMediaIcon = new Image(Main.class.getResource("/images/iconfinder_multimedia-44_2849792.png").toString(), 16, 16, true, true);
/*
其余無關代碼
*/ }