Kenpachi
Kenpachi

Reputation: 673

Cannot run JavaFX application from terminal

When I try to run a JavaFx application from the terminal, it fails to run showing Error: Could not find or load main class com.test.javafx.HelloWorld

Here is my code:

package com.test.javafx;

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 HelloWorld 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);
    }
}

Compilation does not show any error and is successful. But trying to run the application results in Error: Could not find or load main class com.test.javafx.HelloWorld. However when I run a simple application, it runs without any errors.

Here is the javap output:

Compiled from "HelloWorld.java"
public class com.test.javafx.HelloWorld extends javafx.application.Application {
  public com.test.javafx.HelloWorld();
  public void start(javafx.stage.Stage);
  public static void main(java.lang.String[]);
}

I have also tried the following:

java -cp /home/itachi/mycategories/installs/devel/java/jdk/jdk1.8.0_101/jre/lib/ext/jfxrt.jar;." com.test.javafx.HelloWorld

but that doesn't work as well.

Upvotes: 2

Views: 5654

Answers (3)

neotoma
neotoma

Reputation: 1

I just ran into this trying the stock JavaFX HelloWorld example, and it looks like this is a known low-priority bug with OpenJDK/JavaFX. Commenting out the "package" declaration at the top of HelloWorld.java and re-compiling lets it run without problems.

https://bugs.openjdk.java.net/browse/JDK-8163089

I hit this on XUbuntu 16.04/Java 1.8.0_144.

Upvotes: 0

Kenpachi
Kenpachi

Reputation: 673

Fixed the issue

Issue

While installing some software using Linux package manager, OpenJDK JRE Headless (as a dependancy package) was installed. This was confirmed by running:

$ java -version
openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-8u91-b14-3ubuntu1~16.04.1-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)
$ java com.test.javafx.HelloWorld
Error: Could not find or load main class com.test.javafx.HelloWorld

If I run using this OpenJDK JVM, it shows the error.

Fix

Changed the PATH variable so that now Oracle's JVM is default.

$ ${JAVA_HOME}/bin/java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
$ ${JAVA_HOME}/bin/java com.test.javafx.HelloWorld => [Now the program runs]

[After Changing the ~/.bashrc file]
$ . ~/.bashrc
java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
$ java com.test.javafx.HelloWorld => [Now the program runs]

Unknown Factor

Still, I am not sure why OpenJDK JVM throws Error: Could not find or load main class com.test.javafx.HelloWorld, because running Oracle JVM from the same location renders the application.

Upvotes: 3

vdkkj
vdkkj

Reputation: 33

This usually happens when java cannot find your compiled classes in classpath. Try java -cp <path to .class files> HelloWorld

Upvotes: 0

Related Questions