3

I'm using Eclipse 2019-03 working with jdk11. Following the javafx and Eclipse tutorial I've added jdk11 to my module path for my project. I've created a javafx11 user library with all of the javafx jars, and added it to my module path. Reference to my post on Super User

I've created this very simple JavaFX program in Eclipse in Ubuntu 18.04:

import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("hellofx.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 400, 300)); primaryStage.show(); } } 

There are no errors and it compiles, however I get this runtime error:

Error: Could not find or load main class javac Caused by: java.lang.ClassNotFoundException: javac 

How do I fix this so that I can run this basic program?

Edits:

java -version:

openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.04.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.04.1, mixed mode, sharing) 

The VM arguments, as per the tutorial, I've added to my project's run configuration in Eclipse:

--module-path /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml

Code that worked:

 import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) throws Exception{ Group root = new Group(); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root, 400, 300)); primaryStage.show(); } } 

Controller.java

package hellowFx2; import javafx.fxml.FXML; import javafx.scene.control.Label; public class Controller { @FXML private Label label; public void initialize() { String javaVersion = System.getProperty("java.version"); String javafxVersion = System.getProperty("javafx.version"); label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + "."); } } 

hellowFx.fxml

<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Label?> <?import javafx.scene.layout.StackPane?> <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hellofx.Controller"> <children> <Label fx:id="label" text="Label" /> </children> </StackPane> 

Project structure:

hellowFx2

bin src hellowFx2(package) Main.java Controller.java hellowfx.fxml 
8
  • 2
    Have you followed this guide: openjfx.io/openjfx-docs/#IDE-Eclipse ? Commented May 14, 2019 at 7:50
  • @JoséPereda Yes. I forgot to mention that I've added the VM arguments, prior to posting this question, to my run configuration as well. Commented May 14, 2019 at 7:56
  • 3
    Edit your question and post "how" you added those arguments, somehow the order is not right and Eclipse tries to take javac as your main class Commented May 14, 2019 at 8:01
  • So you have installed openjfx, and under that path you have posted you can find javafx-base.jar and other JavaFX modules and their native libraries? When you added your javafx11 library, it looks like this? Still nothing explains your error. Commented May 14, 2019 at 8:25
  • @JoséPereda Yes. To confirm the behavior I created two more hellowFx projects. hellowFx2 behaved slightly differently as it was complaining about the fxml libraries. hellowFx3 I got rid of the hellowfx.fxml and Controller.java files and made the javafx code even simpler(still keeping the module paths and vm arguments the same) and the application worked. I'll put the code that worked in the post. Commented May 14, 2019 at 8:49

1 Answer 1

1

I'll detail my install and setup process in case anyone else has troubles:

Setup for javafx11, jdk11, and Eclipse on Ubuntu 18.04

Install openjdk11 and javafx11 SDK

sudo apt install openjdk-11-jdk openjfx

Be sure to select the SDK version of javafx!

javafx

Check java Version Reads Out the Correct jdk Version

java -version

The output should look like this:

openjdk version "11.0.3" 2019-04-16 OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu218.04.1) OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu218.04.1, mixed mode, sharing) 

Install the Newest Eclipse 2019-03 (4.11)

Download link

When creating a project:

  1. Create a new java project
    • Be sure the jdk used for the project is jdk11
    • Don't create module.info
  2. Right click your project folder and click "Properties". Go to the "Libraries" Tab and left click "Module Path". On the right, select "Add Library" and choose "User Library"
    • name it javafx
    • include all of the .jar files in /path/to/javafx/lib
      • Don't include anyother file type!
  3. Add the newely created libary to the projects module path
  4. In run configuration, found in the run taskbar, select the "Arguments" tab and add this to VM arguments:
    • --module-path /usr/lib/jvm/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml
    • Be sure the path to your javafx is correct!
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.