0

I'm not able to run this code. I got this from oracle tutorials. It's a simple hello world application.earlier I wasn't able to compile it but now after including the path for jfxrt.jar file in classpath I'm able to compile but now not able to run.

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

class A extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler < ActionEvent > () {


            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
}

I'm getting these following errors :

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Appli
cation instance: class A
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm
pl.java:393)
        at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:
47)
        at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
        at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoSuchMethodException: A.<init>()
        at java.lang.Class.getConstructor0(Class.java:2810)
        at java.lang.Class.getConstructor(Class.java:1718)
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherIm
pl.java:275)
        ... 3 more

Help me out,how to resolve this. Thanks

1 Answer 1

1

Make your class public

public class A extends Application

then it should work.

Anyway, do you need another import-Statement? I think the import for ActionEvent is missing:

import javafx.event.ActionEvent;
Sign up to request clarification or add additional context in comments.

4 Comments

hey!! thanks a lot .it worked .now can you explain me the meaning of launch(args),what exactly is it doind?
also primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); actually i'm new to this javafx package,so not aware of these methods and classes.although i know evrythng about javax.swing as i'v used it extensively.Thanks
if your are not familiar with JavaFX and want to use it, I would recommend to start with some basic tutorials. They will give you a good overview. e.g. start here: oracle.com/technetwork/java/javafx/documentation/index.html
hey! i'v already gone through some of these tutorials but they're all about how to create a frame,graphs,animations etc.none of them explains how exactly it's working.like how the control flows in the program.how does the start() gets invoked?how is launch(args) working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.