java命令行运行javafx,使用JavaFX 2.0编译代码(使用命令行)

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

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() {

@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

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:

"%JDK_HOME%\bin\javac" -classpath "%JAVAFX_SDK_HOME%\rt\lib\jfxrt.jar" fxservidor.java

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

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

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() {

@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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值