Skip to main content
added 751 characters in body
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425

Update 2024

This answer was written before the release of modern JavaFX versions that separated JavaFX from most Java distributions. Therefore some information in the original answer is now obsolete (especially the deployment recommendation).

To use JavaFX from the command line for a modern JavaFX version, follow the instructions at openjfx.io.

Alternately, you can (currently) use Azul "JDK FX" or Liberica "Full JDK" (which both include JavaFX), in which case all you need to do is:

javac Fxservidor.java java Fxservidor 

similar to the steps for "Oracle Java 8" from the original answer.

Original Answer

If you are using OpenJDK 8, you will (currently) need to build the JavaFX sources from the OpenJFX repository and and place the resultant jfxrt.jar on your classpath similar to the description for Java 7 defined in this answer.

If you are using OpenJDK 8, you will (currently) need to build the JavaFX sources from the OpenJFX repository and and place the resultant jfxrt.jar on your classpath similar to the description for Java 7 defined in this answer.

Update 2024

This answer was written before the release of modern JavaFX versions that separated JavaFX from most Java distributions. Therefore some information in the original answer is now obsolete (especially the deployment recommendation).

To use JavaFX from the command line for a modern JavaFX version, follow the instructions at openjfx.io.

Alternately, you can (currently) use Azul "JDK FX" or Liberica "Full JDK" (which both include JavaFX), in which case all you need to do is:

javac Fxservidor.java java Fxservidor 

similar to the steps for "Oracle Java 8" from the original answer.

Original Answer

If you are using OpenJDK 8, you will (currently) need to build the JavaFX sources from the OpenJFX repository and place the resultant jfxrt.jar on your classpath similar to the description for Java 7 defined in this answer.

fix syntax typo
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 
added info for java8
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425

Oracle Java 8

If you are using Oracle Java 8 or newer, as pointed out by cayhorstmann in his answer, JavaFX classes are now on the default runtime classpath for an Oracle Java implementation. You can just run javac and java on your program and the JavaFX classes will be found as expected, just like any other class in the JRE.

javac Fxservidor.java java Fxservidor 

OpenJDK 8

If you are using OpenJDK 8, you will (currently) need to build the JavaFX sources from the OpenJFX repository and and place the resultant jfxrt.jar on your classpath similar to the description for Java 7 defined in this answer.

JavaFX 2.x / Java 7

You use the Java Compiler to compile JavaFX programs:

A sample windows batch script for JavaFX 2.x command line development and deployment packaging is provided here.


 

Here is a command I ran on my machine to compile your application (you need to adjust the classpath for your environment):

javac -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor.java 

And here is a command I used to run the compiled class:

java -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar;." Fxservidor 

Note the ;. tokens used to append the current directory to the classpath of the java execution command in Windows (if using a Unix variant, then use :. instead of ;.).

Sample App

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { /** * @param args the command line arguments  */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // Synthetizer os = new SynthetizerSystem.out.println("Ximena""Hello);  } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 

Here is a command I ran on my machineDeployment Recommendation

If you are deploying applications to compileusers, even with Java 8, it is recommended that you package applications using relevant packaging tools (you need to adjust the classpath for your environmente.g. JavaFX ant tasks, javafxpackager, javafx-maven-plugin or javafx-gradle-plugin):

javac -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor.java 

And here is a command I used to run the compiled class:.

java -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar;." Fxservidor 

Update: Note the ;. tokens used to append the current directory If you just want to the classpath of the java executiondo some quick command in Windows (if using a Unix variantline development and testing, then use :. instead of ;.)small programs, those additional packaging tools are not needed and you can just use the simple steps in this answer.

You use the Java Compiler to compile JavaFX programs:

A sample windows batch script for JavaFX command line development and deployment packaging is provided here.


 
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { /** * @param args the command line arguments  */ public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { // Synthetizer os = new Synthetizer("Ximena");  } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 

Here is a command I ran on my machine to compile it (you need to adjust the classpath for your environment):

javac -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor.java 

And here is a command I used to run the compiled class:

java -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar;." Fxservidor 

Update: Note the ;. tokens used to append the current directory to the classpath of the java execution command in Windows (if using a Unix variant, then use :. instead of ;.).

Oracle Java 8

If you are using Oracle Java 8 or newer, as pointed out by cayhorstmann in his answer, JavaFX classes are now on the default runtime classpath for an Oracle Java implementation. You can just run javac and java on your program and the JavaFX classes will be found as expected, just like any other class in the JRE.

javac Fxservidor.java java Fxservidor 

OpenJDK 8

If you are using OpenJDK 8, you will (currently) need to build the JavaFX sources from the OpenJFX repository and and place the resultant jfxrt.jar on your classpath similar to the description for Java 7 defined in this answer.

JavaFX 2.x / Java 7

You use the Java Compiler to compile JavaFX programs:

A sample windows batch script for JavaFX 2.x command line development and deployment packaging is provided here.

Here is a command I ran on my machine to compile your application (you need to adjust the classpath for your environment):

javac -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor.java 

And here is a command I used to run the compiled class:

java -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar;." Fxservidor 

Note the ;. tokens used to append the current directory to the classpath of the java execution command in Windows (if using a Unix variant, then use :. instead of ;.).

Sample App

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class Fxservidor extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } } 

Deployment Recommendation

If you are deploying applications to users, even with Java 8, it is recommended that you package applications using relevant packaging tools (e.g. JavaFX ant tasks, javafxpackager, javafx-maven-plugin or javafx-gradle-plugin).

If you just want to do some quick command line development and testing, of small programs, those additional packaging tools are not needed and you can just use the simple steps in this answer.

Include the current path in the java funtime command to run it.
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425
Loading
added 1623 characters in body
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425
Loading
Source Link
jewelsea
  • 160.5k
  • 15
  • 383
  • 425
Loading