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

大兔子大兔子 提交于 2020-01-09 06:46:28

问题


I'm getting this error

Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Ap
plication
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 13 more

When trying to run my class file, this is the source

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.util.concurrent.Executor;

public class TestApplication extends Application{

    @Override
    public void start(Stage stage) throws Exception {
        new TestApplication();
    }

    public TestApplication() {
        try{
            final Parent root = FXMLLoader.load(Executor.class.getResource("test.fxml"));
            final Stage stage = new Stage(){{
                setScene(new Scene(root, 300, 250));
                setTitle("Test");
                setResizable(false);
                show();
            }};
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

The fxml file contains a simple gui.


回答1:


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!




回答2:


Using java 8 shouldn't give this problem but it did for me

I created my jar initially from Eclipse Export -> Runnable Jar and it was fine. When I moved to Maven it failed with the above.

Comparing the two jars showed that nothing fx related was packaged with the jar (as I'd expect) but that the Eclipse generated manifest had Class-Path: . in it. Getting maven to package the jar with the following worked for me (with Java 8)

 <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.pg.fxapplication.Main</mainClass>
            </manifest>
              <manifestEntries>
                    <Class-Path>.</Class-Path>
                </manifestEntries>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>



回答3:


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>



回答4:


I use maven and I just run this mvn install:install-file -Dfile="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre/lib/jfxrt.jar" -DgroupId=com.oracle.javafx -DartifactId=javafx -Dversion=2.2 -Dpackaging=jar in terminal(Maybe a little difference in Windows.). Then maven will install jfxrt.jar then you can simply reference it as

<dependency>
  <groupId>com.oracle.javafx</groupId>
  <artifactId>javafx</artifactId>
  <version>2.2</version>
</dependency> 



回答5:


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"



回答6:


IntelliJ and maybe other IDE's do not refactor your Run/Debug configuration. You must manually change your package name preceding the name of your Main class. For instance, change 'sample.Main' to 'com.company.package.ui.Main' so it will launch correctly next time you try to run it. The IDE might have already marked the Run/Debug button with a red cross because it couldn't find the main class. It also gives a warning when you open the Run/Debug configuration.




回答7:


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 $?



回答8:


I had a problem of not finding the Pair class from javafx.

It seems that vanilla eclipse (without the e(fx)clipse extension) doesn't search the javaFX runtime jar included with java.

I just added to my eclipse project build path this external jar (or if you are in a debug configuration, add the external jar in the JRE tab of the debug configuration):

$JAVA_HOME/jre/lib/ext/jfxrt.jar

(replace JAVA_HOME with your jdk folder)

For me it was in /installs/jdk1.8.0_211/jre/lib/ext/jfxrt.jar




回答9:


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

Bundle-ClassPath: .,
jfxrt.jar


来源:https://stackoverflow.com/questions/16868446/javafx-exception-in-thread-main-java-lang-noclassdeffounderror-javafx-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!