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

I tried compiling this using the command

javac -cp /opt/jdk8/jdk1.8.0_11/jre/lib/ext/jfxrt.jar HelloWorld.java

It gives several errors saying :

javafx.application package doesn't exist javafx.scene package doesn't exist

I want some help ... I am new to javafx... and I don't want to use any IDE

Joel
  • 7,401
  • 4
  • 52
  • 58
  • what does it print when you run `javac -version` and `java -version` – janih Oct 23 '14 at 14:01
  • @janih i found the problem.. while running applet viewer i had to change my default java version from oraclejdk to openjdk.. i switched it back and it works fine now – user2951609 Oct 24 '14 at 09:29

1 Answers1

2

You don't need to add jfxrt.jar to classpath when compiling. Just be sure that the javac command is pointing to the /opt/jdk8 installation that you have.

I tried your code example, it ran just fine, opened a small window with a button in it:

~$ javac -version
javac 1.8.0_25
~$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
~$ javac HelloWorld.java
~$ java HelloWorld
Hello World!
janih
  • 2,214
  • 2
  • 18
  • 24