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 :/
"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: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 extendsApplication
and displays a simple window. Let's also say that, when the code is modular, the module looks like:And your project structure looks like this:
Then your command line will look like one of the following (Windows oriented):
Non-modular
Exploded Directory
Jar File
Note: Requires
Main-Class
attribute in the manifest.Modular
Exploded Directory
Jar File
Or if the Jar was created/updated with
--main-class
Notes:
<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