Compiling and running with JavaFX 2.1

This question somewhat duplicates compile javafx 2.0 manually.

This answer is specifically for JavaFX 2 versions before the release of Oracle Java 8. For Oracle JavaFX 8+, the JavaFX runtime is on the classpath, so you don’t need to explicitly add it when compiling or JavaFX running applications.

Java includes JavaFX in jdk7u6 and above (for Windows and Linux) and jdk7u4 and above (for OSX).

Download and use jdk7u6+ and you won’t need to specify the jfxrt.jar file in any classpath and all of your JavaFX related classpath issues should go away.

Here is a link to an early binary build of jdk7u6.

For JavaFX 2.1 on Windows you do need to include the jfxrt.jar lib in your classpath for compile (NetBeans will do this automatically if you use it’s JavaFX platform settings) and (if you haven’t packaged your app correctly using the javafxpackager or JavaFX ant tasks), also at runtime.

JavaFX 2.1 for Linux is a pre-release (in case you are using that). For the Linux pre-release you would only have to include jfxrt.jar in your classpath at both compile and runtime if the JavaFX runtime on Linux was not set up correctly.

Here is an example of a command line compilation and execution of a JavaFX program under windows.

Launch an editor:

C:\dev\test>notepad HelloWorld.java

Paste the following code and save it:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class HelloWorld extends Application {
  public static void main(String[] args) {
    launch(args);
  }
  @Override
  public void start(Stage stage) {
    stage.setScene(new Scene(new Label("Hello World")));
    stage.show();
  }
}

Compile and run it JavaFX 2.2:

C:\dev\test>javac -cp "\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld.java
C:\dev\test>java -cp ".;\Program Files\Oracle\JavaFX 2.2 Runtime\lib\jfxrt.jar" HelloWorld

For Oracle Java 8+, the explicit JavaFX classpath specifier is not required:

C:\dev\test>javac HelloWorld.java
C:\dev\test>java HelloWorld

Note that usually rather than just compiling the code and running it, you would also package the code with javafxpackager or the javafx ant tasks. These tasks will embed a launcher class into your packaged app which will detect the version and location of the JavaFX runtime so that you don’t need to specify the jfxrt.jar location unless you want to override the default location for the platform.

Leave a Comment