问题
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: NetBeans 10.0
Paths:
JAVA_HOME = C:/Program Files/Java/jdk-11.0.2
PATH_TO_FX = C:/Program Files/Java/javafx-sdk-11.0.2/lib
Inside Path system variable add %JAVA_HOME%/bin
In NetBeans I created Java Application project named JFXDev which contains one package com. Inside com package is one main class with following code:
package com;
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 Main extends Application {
@Override
public void start(Stage primaryStage) {
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);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Properties of projects are modified:
1) Libraries -> Modulepath added path C:/Program Files/Java/javafx-sdk-11.0.2/lib
2) Libraries -> Classpath added path C:/Program Files/Java/javafx-sdk-11.0.2/lib/javafx.controls.jar
3) Run -> VM Options: added --add-modules=javafx.controls,javafx.fxml
When I start application with NetBeans it works perfectly but I was unable to start application from dist folder. I tried following commands in cmd:
Command:
java -jar JFXDev.jar
- Response:
Error: could not find or load main class com.Main
- Response:
Command:
java --module-path '%PATH_TO_FX% --add-modules=javafx.comntrols JFXDev
- Response:
Error: Could not find or load main class Files/Java/javafx-sdk-11.0.2/lib
- Response:
Command:
java --module-path %PATH_TO_FX% --add-modules=javafx.comntrols Main
- Response:
Error: Could not find or load main class Files/Java/javafx-sdk-11.0.2/lib
- Response:
Command:
java --module-path %PATH_TO_FX% --add-modules=javafx.comntrols com.Main
- Response:
Error: Could not find or load main class Files/Java/javafx-sdk-11.0.2/lib
- Response:
- Command:
java --module-path "C:/Program Files/Java/javafx-sdk-11.0.2/lib/" --add-modules=javafx.comntrols com.Main
- Response:
Error: Could not find or load main class Main
- Response:
Later I added module-info.java
file:
module JFXDev {
requires javafx.controls;
exports com;
}
But it doesn't make any difference.
What I'm doing wrong, any advice or suggestion?
I tried steps from https://openjfx.io/openjfx-docs/#install-javafx but I get same error :/
回答1:
"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 <path-to-jar-file>
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:
\---<project-directory>
+---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
<project-directory>
is the working directory. -p
is shorthand for--module-path
-m
is shorthand for--module
-cp
is shorthand for--class-path
or-classpath
来源:https://stackoverflow.com/questions/55652036/how-to-start-javafx-11-application-outside-ide