使用JavaFX 2.0编译代码(使用命令行) [英] Compile code using JavaFX 2.0 (using command line)
问题描述
我想知道如何使用JavaFX从Windows shell中编译代码。
我有这个代码在 fxservidor.java
:
public class Fxservidor extends应用程序{
/ **
* @param args命令行参数
* /
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 );
}
});
StackPane root = new StackPane();
root.getChildren()。add(btn);
primaryStage.setScene(new Scene(root,300,250));
primaryStage.show();
}
}
em> Oracle Java 8
如果你使用的是Oracle Java 8或更新版本,正如cayhorstmann在他的回答中指出的,JavaFX类现在是默认的Oracle Java实现的运行时类路径。您可以在程序上运行 javac
和 java
,JavaFX类将按预期找到,就像任何其他
javac Fxservidor.java
java Fxservidor
OpenJDK 8
如果您使用的是OpenJDK 8,将(当前)需要从OpenJFX存储库构建JavaFX源,并将结果jfxrt .jar在类路径上类似于此答案中定义的Java 7的描述。
JavaFX 2.x / Java 7
您使用 Java Compiler 编译JavaFX程序:
%JDK_HOME%\bin\javac-classpath%JAVAFX_SDK_HOME%\rt\lib\jfxrt.jarfxservidor.java
分别用安装的JDK和JavaFX SDK的路径替换JDK_HOME和JAVAFX_SDK_HOME占位符。
提供了用于JavaFX 2.x命令行开发和部署打包的示例窗口批处理脚本这里。
这是我在我的机器上运行以编译应用程序的命令(需要调整环境的类路径) p>
javac -classpath\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jarFxservidor.java
这是一个用来运行编译类的命令:
java -classpath\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar ;. Fxservidor
注意
;。
将当前目录附加到Windows中java执行命令的类路径(如果使用Unix版本,则使用:。
而不是code>)。
示例应用程序
您的程序的版本将编译:
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应用程序{
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 Scene(root,300,250));
primaryStage.show();
}
}
部署建议
如果您要将应用程式部署到使用者,建议您使用相关的打包工具打包应用程序(例如 JavaFX ant任务, javafxpackager , javafx-maven-plugin 或 javafx-gradle-plugin )。
如果你只是想做一些快速的命令行开发和测试,小程序,那些额外的包装工具是不需要的,你可以使用简单的步骤回答。
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<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
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
andjava
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<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.
这篇关于使用JavaFX 2.0编译代码(使用命令行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!