简体   繁体   中英

How to run JavaFx application from command line

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class app 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();
   }


   public static void main(String[] arguments)
   {
      Application.launch(arguments);
   }
}

I have this JavaFx code that I want to run from the command line. I tried the command javac -d app.java it gives me an error: no source files and if I try with the command java -cp D:\javafx-sdk-13.0.2\lib\jfxrt.jar app.java. The error message is app not found. What am I doing wrong and how can I fix it so that GUI application runs from the command line?

First of all, please make sure you know how to run a regular java application .

NOTE: If you are using JDK version 10 or below, you can directly run the javafx application as you do with other java programs as javafx is preinstalled in JDK 10 or below. In order to check your JDK version, type the following command in your command line:

javac -version

Please note that this tutorial is for WINDOWS ONLY.

As mentioned earlier, you need the following code to compile javafx application from the command line:

Step 1: Type "cd" and then write the path starting from "C:" (C drive) to the location where you have saved your code above in.java format.

Step 2: Then you need to press Enter so that the command line would open that path. If it yields an error, please recheck the path you passed. If in case the path has white spaces in between somewhere. Please surround it with double quotes(" ") after writing cd. Then try again.

Step 3: Type the following in your command line in the folder where your.java file is:

javac --module-path "Path to your javafx lib folder" --add-modules javafx.controls,javafx.fxml YourFile.java

In the double quotes above, you need to write the path to the lib folder in your javafx-sdk folder which you installed from openjfx.io .

The above code will compile your.java file into.class file, a code format that can be interpreted by JVM (java virtual machine). More on class file in java: Java Class File .

After this, you need java to interpret and run the.class file you just created using the above syntax.

In order to do that, you just need to remove "c" from "javac" in the above code and also remove the.java extension. Other things remain the same.

java --module-path "Path to your javafx lib folder" --add-modules javafx.controls,javafx.fxml YourFile

This should work perfectly and help you run your javafx application from cmd.

Now in case it feels a burden to you to write this code again and again. You could do the following.

An environment variable could help you compile the above codes into a single word which you can type in your command line surrounded by %.

In order to set your environment variable, go to the search bar in the bottom left corner of the screen. Then type environment variables in the search bar.

You should see something like this .

You can click the open button in order to open it in the control panel. Then follow these steps:

  1. Go to tab: Advanced, which will probably be the default tab when you open it.
  2. Click "Environment Variables" in the bottom right.
  3. Click "New" in the 2nd bottom panel.
  4. set "Variable name" as "JAVA_FX"
  5. set "Variable value" as the following:

--module-path "Path to your javafx lib folder" --add-modules javafx.controls,javafx.fxml Please write the full correct path to your lib folder in your openjfx folder that you downloaded. And make sure it is inside double quotes .

After doing that, click "OK" and then close the control panel. Now you need to restart the command line application. Without restarting, it will not show the environment variable that you just set. Just close it and open it again.

Now type the following in your command line: echo %JAVA_FX%

This should show you the variable value that you had just set. If it prints JAVA_FX, then please consider re-referring the above steps and checking for any path errors you made while typing the path to the lib folder in variable value in environment variables.

If it shows the correct value, continue:

Now you can type the following command in your command line. Make sure you are in the folder where your.java file is saved, if not please direct to the folder by using the "cd" command mentioned earlier:

javac %JAVA_FX% YourFile.java Make sure to change the "YourFile" name to the name of your java file.

Then it should compile the java file into a java class file. Then you type the following in your command line: java %JAVA_FX% YourFile

This should work fine.

Hope you learned something new, and if you have any further queries, please mention them in the comments section.

This is the command I used to make it run from the command line

// to compile:

javac --module-path D:\javafx-sdk-13.0.2\lib --add-modules javafx.controls app.java

// to run:

java --module-path D:\javafx-sdk-13.0.2\lib --add-modules javafx.controls app

It does not work any differently than running any other java program from the command line. You just have to get into the directory where your class app is located, and then type in the following commands: javac app.java if there is only that class in the directory. If there is more than one java file, type javac *.java . Then, after the files are compiled, type java, followed by the name of the class with the main method in it. In your case, this looks like java app . This should run your javafx code from the command line. On a side note, class names should follow the PascalCase naming convention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM