Compile code using JavaFX 2.0 (using command line)

2019-01-01 07:53发布

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

5条回答
还给你的自由
2楼-- · 2019-01-01 08:04

FWIW, in JDK 8, the JavaFX jar seems to be available without having to put it on the classpath.

path/to/jdk1.8.0-b96/bin/javac HelloWorld.java
path/to/jdk1.8.0-b96/bin/java HelloWorld

works like you'd expect.

查看更多
临风纵饮
3楼-- · 2019-01-01 08:08
javac -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor.java

I ran the above command on Ubuntu Linux with JavaFX2.0 beta it compiled it's class files but when I tried to run it with this command it did not work.

java -classpath "\Program Files\Oracle\JavaFX 2.1 Runtime\lib\jfxrt.jar" Fxservidor

This is the error message I get:

Error: Could not find or load main class Fxservidor

查看更多
不再属于我。
4楼-- · 2019-01-01 08:10

Raw but simple solution is to put a copy of jfxrt.jar file into the .../jre7/lib/ext directory under your java installation.

Then, you should always have it available on your classpath.

Not recommended for distributing jfx apps, of course, but convenient for developer playing around...

(For explanation, see: http://docs.oracle.com/javase/tutorial/ext/basics/install.html)

查看更多
宁负流年不负卿
5楼-- · 2019-01-01 08:15

In macbook, osx, etc, you can compile:

javac -cp "/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jfxrt.jar" Fxservidor.java

and run:

java -cp ".:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jfxrt.jar" Fxservidor

Note the .: instead of .;

查看更多
孤独总比滥情好
6楼-- · 2019-01-01 08:21

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

查看更多
登录 后发表回答