Hello JavaFX 2.0: Introduction by Command Line
main
function). HelloWorld.java (I: Bare Minimum)
package dustin.examples; import javafx.application.Application; import javafx.stage.Stage; /** * Simple JavaFX Hello World example. * * @author Dustin */ public class HelloWorld extends Application { @Override public void start(final Stage stage) throws Exception { throw new UnsupportedOperationException("JavaFX example not supported yet."); } }
The previous code snippet shows the importing of two JavaFX classes (Application and Stage) When the above code is compiled with javac without placing the JavaFX libraries on the classpath, errors similar to the following occur.
HelloWorld.java:3: error: package javafx.application does not exist import javafx.application.Application; ^ HelloWorld.java:4: error: package javafx.stage does not exist import javafx.stage.Stage; ^ HelloWorld.java:11: error: cannot find symbol public class HelloWorld extends Application ^ symbol: class Application HelloWorld.java:14: error: cannot find symbol public void start(final Stage stage) throws Exception ^ symbol: class Stage location: class HelloWorld HelloWorld.java:13: error: method does not override or implement a method from a supertype @Override ^ 5 errors
The obvious solution is to place the apropos JavaFX library on the classpath of the compiler. In my case, the JavaFX SDK and JAR needed to build this code is C:\Program Files\Oracle\JavaFX 2.0 SDK\rt\lib\jfxrt.jar.
The next code listing builds upon the previous code snippet and is adapted from the example provided in the Application class’s class-level Javadoc documentation.
HelloWorld.java (II: Adapted from Application’s Javadoc)
package dustin.examples; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Circle; import javafx.stage.Stage; /** * Simple JavaFX Hello World example. * * @author Dustin */ public class HelloWorld extends Application { @Override public void start(final Stage stage) throws Exception { final Circle circ = new Circle(40, 40, 30); final Group root = new Group(circ); final Scene scene = new Scene(root, 400, 300); stage.setTitle("Hello JavaFX 2.0!"); stage.setScene(scene); stage.show(); } }
The JavaFX application shown above can be deployed to a web browser, but I’m going to instead focus on running it from the command line. To do this, a main function is added to the JavaFX application as shown in the next version.
HelloWorld.java (III: Added ‘main’ Function)
package dustin.examples; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.shape.Circle; import javafx.stage.Stage; /** * Simple JavaFX Hello World example. * * @author Dustin */ public class HelloWorld extends Application { @Override public void start(final Stage stage) throws Exception { final Circle circ = new Circle(40, 40, 30); final Group root = new Group(circ); final Scene scene = new Scene(root, 400, 300); stage.setTitle("Hello JavaFX 2.0!"); stage.setScene(scene); stage.show(); } /** * Main function used to run JavaFX 2.0 example. * * @param arguments Command-line arguments: none expected. */ public static void main(final String[] arguments) { Application.launch(arguments); } }
Only a single line is required in the main
function. That line is a call to the static method Application.launch(String…) with the command-line arguments passed to it. This application can now be executed and appears as shown in the screen snapshot that follows.