使用 JavaFX 2.0 编译代码(使用命令行) [英] Compile code using JavaFX 2.0 (using command line)

查看:29
本文介绍了使用 JavaFX 2.0 编译代码(使用命令行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何从 Windows shell 使用 JavaFX 编译代码.

I wonder how to compile code using JavaFX, from a Windows shell.

我在 fxservidor.java 中有这个代码:

I have this code in fxservidor.java:

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

推荐答案

Oracle Java 8

如果您使用的是 Oracle Java 8 或更新版本,正如 cayhorstmann 在他的回答中指出的那样,JavaFX 类现在位于 Oracle Java 实现的默认运行时类路径上.您只需在您的程序上运行 javacjava,就会按预期找到 JavaFX 类,就像 JRE 中的任何其他类一样.

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

如果您使用的是 OpenJDK 8,则您(当前)需要 构建来自 OpenJFX 存储库的 JavaFX 源,并将生成的 jfxrt.jar 放在您的类路径上,类似于此答案中定义的 Java 7 描述.

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

您使用 Java 编译器编译 JavaFX 程序:

You use the Java Compiler to compile JavaFX programs:

"%JDK_HOME%injavac" -classpath "%JAVAFX_SDK_HOME%
tlibjfxrt.jar" fxservidor.java 

分别用您安装的 JDK 和 JavaFX SDK 的路径替换 JDK_HOME 和 JAVAFX_SDK_HOME 占位符.

Replace the JDK_HOME and JAVAFX_SDK_HOME placeholders with the paths to your installed JDK and JavaFX SDK respectively.

提供了用于 JavaFX 2.x 命令行开发和部署打包的示例 Windows 批处理脚本 此处.

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 FilesOracleJavaFX 2.1 Runtimelibjfxrt.jar" Fxservidor.java

这是我用来运行编译类的命令:

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

java -classpath "Program FilesOracleJavaFX 2.1 Runtimelibjfxrt.jar;." Fxservidor

注意 ;. 标记用于将当前目录附加到 Windows 中 java 执行命令的类路径(如果使用 Unix 变体,则使用 :. 代替;.).

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 ;.).

示例应用

这是您的程序的修改版本,它将编译:

Here is a modified version of your program which will compile:

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

}

部署建议

如果您要向用户部署应用程序,即使使用 Java 8,建议您使用相关打包工具打包应用程序(例如 JavaFX 蚂蚁任务javafxpackagerjavafx-maven-pluginjavafx-gradle-plugin).

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.

这篇关于使用 JavaFX 2.0 编译代码(使用命令行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆