Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 361
Archey Avatar asked Jun 01 '13 02:06

Archey


People also ask

How do you fix No Class Def Found error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.


1 Answers

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!

like image 101
Rob Stoecklein Avatar answered Oct 08 '22 03:10

Rob Stoecklein