JavaFX Exception in thread “main” java.lang.NoClassDefFoundError: javafx/application/Application

前端 未结 11 1276
半阙折子戏
半阙折子戏 2020-12-01 08:09

I\'m getting this error

Exception in thread \"main\" java.lang.NoClassDefFoundError: javafx/application/Ap
plication
        at java.lang.ClassLoader.defineC         


        
11条回答
  •  情歌与酒
    2020-12-01 08:23

    I've worked on this very same issue for the past few hours. Even though I haven't seen it written explicitly, it appears that you MUST use one of the JavaFX packaging tools, which is either an Ant task or the javafxpackager executable. (See http://docs.oracle.com/javafx/2/deployment/packaging.htm, section 5.3.1). The NetBeans IDE uses Ant to package the code. (I'm using IntelliJ)

    When you use one of those packaging methods, in addition to all of your application's code and resources, it also adds the following to your output JAR file:

    /com/javafx/main/Main$1.class
    /com/javafx/main/Main$2.class
    /com/javafx/main/Main.class
    /com/javafx/main/NoJavaFXFallback.class
    

    With these in place, you can run the app from the command line:

    java -jar outjar.jar
    

    and all works fine. If I remove the extra com.javafx.main files, the app does not run.

    To double-check this, I looked at all four JAR files in the JavaFX samples (BrickBreaker, Ensemble, FXML-LoginDemo, and SwingInterop). They all have the "extra" files, too.

    For my small test app, I used this command line to build an "executable" JAR file:

    javafxpackager -createjar -appclass sample.Main -outfile outjar -v -nocss2bin -srcdir C:\workspaces\garoup1\out\production\javafx1
    

    Hope this helps!

提交回复
热议问题