JavaFX Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application
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.
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!
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
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>