DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Java
  4. Hello JavaFX 2.0: Introduction by Command Line

Hello JavaFX 2.0: Introduction by Command Line

Dustin Marx user avatar by
Dustin Marx
·
Dec. 17, 11 · Interview
Like (0)
Save
Tweet
Share
9.06K Views

Join the DZone community and get the full member experience.

Join For Free

I looked at a simple JavaFX version of the ubiquitous Hello World example from a NetBeans 7.1 beta perspective in the blog post Hello JavaFX 2.0: Introduction by NetBeans 7.1 beta. In this post, I look at a slightly different version of Hello World implemented with JavaFX using only command-line tools.

The JavaFX 2.0 API documentation includes the class description for the javafx.application.Application class and this is a good place to start. The Javadoc documentation for the Application class provides an example of an effective class usage description. This class description describes a JavaFX application's life cycle and even provides a code sample with an image showing how the sample renders. I'll work up to that same sample in this post.

The Application class's Javadoc documentation describes the central role of this class: "Application class from which JavaFX applications extend." The start(Stage) method is the most interesting in the Application class as it is the "main entry point for all JavaFX applications." It is an abstract method and so must be overridden by extending classes. The next code listing shows a minimal implementation that will compile but not do anything (it doesn't even have a 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.

Conclusion

This blog post has demonstrated writing and running a simple JavaFX application using only command-line tools. Proving that JavaFX 2.0 has put the 'Java' back into JavaFX, the examples in this post were compiled and executed with the typical Java compiler and Java launcher used for "normal" Java application. More complex JavaFX applications may benefit from more specific tools, but this one was compiled and executed solely with the standard Java tools.

 

From http://marxsoftware.blogspot.com/2011/12/hello-javafx-20-introduction-by-command.html

JavaFX application Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • File Uploads for the Web (1): Uploading Files With HTML
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Spinnaker vs. Argo CD: Best Tools for Continuous Delivery
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: