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

前端 未结 11 1167
半阙折子戏
半阙折子戏 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:14

    I know it may not be the "proper" way of launching an javafx application but i have struggled with this problem for some time and came up with a workaround that does not require to use any external packaging applications, force you to use ant or maven plugin (which conflicts with the shade plugin) etc...

    The solution uses Utils4j to load jfxrt dynamically at runtime. You cannot load it in a class extending javafx.application.Application, do it in a separate class and name it for example: Launcher

    import org.fuin.utils4j.Utils4J
    
    public class Launcher {
    
        public static void main(String[] args) {
            Utils4J.addToClasspath("file:///"+System.getProperty("java.home")+ File.separator+"lib"+File.separator+"jfxrt.jar");
    
    // CODE TO RUN YOUR CLASS THAT EXTENDS javafx.application.Application goes here.
    
        }
    }
    

    you can include Utils4j with your project (if using maven):

        <dependency>
            <groupId>org.fuin</groupId>
            <artifactId>utils4j</artifactId>
            <version>0.7.0</version>
        </dependency>
    
    0 讨论(0)
  • 2020-12-01 08:14

    in eclipse environment: add jfxrt.jar to Bundle-ClassPath in MANIFEST.MF file.

    Bundle-ClassPath: .,
    jfxrt.jar
    
    0 讨论(0)
  • 2020-12-01 08:22

    I had the same problem and below steps helped me to solve it,

    Adding the vm arguments while running the application,

    --module-path  /home/user/Java-libraries/openjfx-11.0.2_linux-x64_bin-sdk/javafx-sdk-11.0.2/lib/ --add-modules javafx.controls,javafx.fxml
    

    Note:

    • The --module-path will contain the jars of Java fx
    • I used open-jdk 13

    Configure this in your eclipse (If you are using so) or you can just compile and run like,

    Compile

    javac --module-path /path/to/java-fx-libs/ --add-modules javafx.controls,javafx.fxml *.java
    

    Run

    java --module-path /path/to/java-fx-libs/ --add-modules javafx.controls,javafx.fxml MyMainClass
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-01 08:23

    I'm developing on Linux a simple WebApp i got the same error, but is really easy to fix it (assuming you are developing on the command line as myself).

    cat compile.sh 
     #!/bin/bash
    
     /usr/lib/jvm/jdk1.7.0_25/bin/javac WebViewSample.java  -cp /usr/lib/jvm/jdk1.7.0_25     
     /jre/lib/jfxrt.jar
    
     $ cat run.sh 
     #!/bin/sh
     JAVA_HOME=/usr/lib/jvm/jdk1.7.0_25/bin/
     CLASSPATH=/usr/lib/jvm/jre1.7.0_25/lib/jfxrt.jar:. 
    
     $JAVA_HOME/java -cp $CLASSPATH WebViewSample $* 2>&1 /dev/null | awk -F\| '{ print  $2"|"$3  ; exit $1 }'
    
     exit $?
    
    0 讨论(0)
  • 2020-12-01 08:27

    If you are using netbeans like me and you have two versions of JDK installed, then you should change the classpath to the appropriate java installation in the config file. This is also a bug in Netbeans: There are two ways to do it:
    Either start NetBeans with --jdkhome by executing this:

    "C:\Program Files\NetBeans Dev 201402160001\bin\netbeans.exe" --jdkhome "C:\Program Files\Java\jdk1.7.0_51"
    

    Or set the "netbeans_jdkhome" property in /etc/netbeans.conf e.g.

    # Default location of JDK, can be overridden by using --jdkhome <dir>:
    netbeans_jdkhome="C:\Program Files\Java\jdk1.7.0_51"
    
    0 讨论(0)
提交回复
热议问题