用Idea寫了一個JavaFX項目,建立artifacts,build artifacts,運行build出來的exe可執行文件時老是遇到 class not found的錯誤,以下圖測試
一開始根據提示覺得是class路徑沒設置好,但怎麼改都是同樣的錯誤,網上也搜不到跟我遇到一樣問題的人。ui
後來新建了個JavaFX項目,不用Maven,發現能夠正常部署,能夠找到Main類。spa
最後兩個項目之間一點點地修改,一點點地比對,發現問題出在靜態屬性的定義並初始化上,好比下面代碼中有四個靜態屬性而且都在類加載時進行初始化code
控制變量法式地測試發現,單純定義了靜態屬性的話,沒有問題能夠找到Main類,但若是在定義語句後面初始化賦值,就會致使上面圖中的class not found的錯誤blog
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);
/*
其他無關代碼
*/ }
若是定義了成員屬性以下面的logger而且在定義語句後面初始化賦值,運行exe文件時會發生下圖中的錯誤:Error invoking method。部署