How to start JavaFX 11 application outside IDE?

前端 未结 1 761
庸人自扰
庸人自扰 2021-01-06 14:43

I downloaded:
- OpenJDK 11.0.2
- JavaFX SDK 11.0.2
Both files are extracted to path C:/Program Files/Java/

OS: Windows 10
IDE:

1条回答
  •  走了就别回头了
    2021-01-06 15:05

    "Actual" Answer

    Some of those errors you've provided indicate a problem with spaces in your arguments (e.g. C:/Program Files/...). Surround %PATH_TO_FX% with quotes: "%PATH_TO_FX%". Then, as you said in a comment, the correct command line for you is:

    java -p "%PATH_TO_FX%" --add-modules javafx.controls -jar  
    

    Rest of Answer

    From the information you've provided, it's difficult (for me at least) to tell what exactly the problem is. Instead, I'll give some examples of launching a JavaFX application from the command line, both modular and non-modular.

    Let's say you have a project with one class—the main class—named com.example.Main. This class extends Application and displays a simple window. Let's also say that, when the code is modular, the module looks like:

    module app {
        requires javafx.controls;
        exports com.example to javafx.graphics;
    }
    

    And your project structure looks like this:

    \---
        +---out
        |   +---artifacts
        |   |       app-1.0.jar (modular or non-modular)
        |   |
        |   \---classes
        |       |   module-info.class (when modular)
        |       |
        |       \---com
        |           \---example
        |                   Main.class
        |
        \---src
            |   module-info.java (when modular)
            |
            \---com
                \---example
                        Main.java
    

    Then your command line will look like one of the following (Windows oriented):

    Non-modular

    Exploded Directory

    java -p "%PATH_TO_FX%" --add-modules javafx.controls -cp out\classes com.example.Main
    

    Jar File

    java -p "%PATH_TO_FX%" --add-modules javafx.controls -jar out\artifacts\app-1.0.jar
    

    Note: Requires Main-Class attribute in the manifest.

    Modular

    Exploded Directory

    java -p "%PATH_TO_FX%;out\classes" -m app/com.example.Main
    

    Jar File

    java -p "%PATH_TO_FX%;out\artifacts" -m app/com.example.Main
    

    Or if the Jar was created/updated with --main-class

    java -p "%PATH_TO_FX%;out\artifacts" -m app
    

    Notes:

    • The above assumes that is the working directory.
    • -p is shorthand for --module-path
    • -m is shorthand for --module
    • -cp is shorthand for --class-path or -classpath

    0 讨论(0)
提交回复
热议问题