简体   繁体   English

如何从命令行运行 JavaFx 应用程序

[英]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.我有这个 JavaFx 代码,我想从命令行运行。 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. 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?我做错了什么,如何修复它以便 GUI 应用程序从命令行运行?

First of all, please make sure you know how to run a regular java application .首先,请确保您知道如何运行常规的 java 应用程序

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.注意:如果您使用的是 JDK 版本 10 或更低版本,则可以像运行其他 java 程序一样直接运行 javafx 应用程序,因为 javafx 预安装在 JDK 或以下版本中。 In order to check your JDK version, type the following command in your command line:为了检查您的 JDK 版本,请在命令行中键入以下命令:

javac -version

Please note that this tutorial is for WINDOWS ONLY.请注意,本教程仅适用于 WINDOWS。

As mentioned earlier, you need the following code to compile javafx application from the command line:如前所述,您需要以下代码从命令行编译 javafx 应用程序:

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.第1步:输入“cd”,然后将路径从“C:”(C盘)开始写入你上面保存代码的位置为.java格式。

Step 2: Then you need to press Enter so that the command line would open that path.第 2 步:然后您需要按 Enter,以便命令行打开该路径。 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.请在写入 cd 后用双引号 (" ") 将其括起来。 Then try again.然后再试一次。

Step 3: Type the following in your command line in the folder where your.java file is:第 3 步:在您的.java 文件所在文件夹的命令行中键入以下内容:

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 .在上面的双引号中,您需要在您从openjfx.io安装的 javafx-sdk 文件夹中写入 lib 文件夹的路径。

The above code will compile your.java file into.class file, a code format that can be interpreted by JVM (java virtual machine).上面的代码会将你的.java文件编译成.class文件,一种可以被JVM(java虚拟机)解释的代码格式。 More on class file in java: Java Class File .更多关于 java 中 class 文件的信息: Java Class 文件

After this, you need java to interpret and run the.class file you just created using the above syntax.在此之后,您需要 java 来解释和运行您刚刚使用上述语法创建的.class 文件。

In order to do that, you just need to remove "c" from "javac" in the above code and also remove the.java extension.为此,您只需在上述代码中从“javac”中删除“c”,并删除.java 扩展名。 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.这应该可以完美运行并帮助您从 cmd 运行 javafx 应用程序。

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.为了设置您的环境变量,go 到屏幕左下角的搜索栏。 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. Go 到选项卡:高级,这可能是您打开它时的默认选项卡。
  2. Click "Environment Variables" in the bottom right.点击右下角的“环境变量”。
  3. Click "New" in the 2nd bottom panel.单击第二个底部面板中的“新建”。
  4. set "Variable name" as "JAVA_FX"将“变量名”设置为“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. --module-path "Path to your javafx lib folder" --add-modules javafx.controls,javafx.fxml请在您打开的.fx文件夹中写入您下载的lib文件夹的完整正确路径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%现在在命令行中输入以下内容: 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.如果它打印 JAVA_FX,那么请考虑重新参考上述步骤并检查您在环境变量的变量值中键入 lib 文件夹的路径时是否存在任何路径错误。

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:确保您在保存.java 文件的文件夹中,如果没有,请使用前面提到的“cd”命令直接进入该文件夹:

javac %JAVA_FX% YourFile.java Make sure to change the "YourFile" name to the name of your java file. javac %JAVA_FX% YourFile.java确保将“YourFile”名称更改为 java 文件的名称。

Then it should compile the java file into a java class file.然后它应该将 java 文件编译成 java class 文件。 Then you type the following in your command line: java %JAVA_FX% YourFile然后在命令行中键入以下内容: 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 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 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.它与从命令行运行任何其他 java 程序没有任何不同。 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.您只需进入 class app所在的目录,然后键入以下命令: javac app.java如果目录中只有 class。 If there is more than one java file, type javac *.java .如果有多个 java 文件,请键入javac *.java Then, after the files are compiled, type java, followed by the name of the class with the main method in it.然后,编译完文件后,输入 java,然后输入 class 的名称,其中包含 main 方法。 In your case, this looks like java app .在您的情况下,这看起来像java app This should run your javafx code from the command line.这应该从命令行运行您的 javafx 代码。 On a side note, class names should follow the PascalCase naming convention.另一方面,class 名称应遵循PascalCase命名约定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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