Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime error for a basic javafx11 application using jdk11 and Eclipse 2019-03 (4.11.0)

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
like image 236
Jon Avatar asked Mar 28 '26 03:03

Jon


1 Answers

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!
like image 119
Jon Avatar answered Mar 29 '26 15:03

Jon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!