Search code examples
javafxprogram-entry-pointpackager

why javafxpackager -could not load main class?


I have a silly question since I've searched the databse here but I couldn't find the answer. I'm new to javaFX and all so ... please help me out!.

Here's the code I used. It's a sample code though.

package helloworld;

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 {
    public static void main(String[] args) {
        launch(args);
    }


    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");
        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);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();

    }

}

After compiling it from within the directory .\helloworld using javac HelloWorld.java, I run the following command:

PS D:\documents\javafx\helloworld> javafxpackager -createjar -appclass HelloWorld -srcdir . -outdir out -outfile hello.jar -v

But then, when I cd to out and run java -jar hello.jar I get this:

PS D:\documents\javafx\helloworld\out> java -jar hello.jar Error: Could not find or load main class HelloWorld

What happened or did I do something wrong?

Any suggestion/explanation is really appreciated.

Thanks all.

Zestos.


Solution

  • Your application class is in package helloworld, so to reference it you should use the fully qualified name of helloworld.HelloWorld.

    Here is a complete example using the example HelloWorld application from your question. I tried this on OS X 10.8 with Oracle Java 8u25 installed and it worked for me.

    $ 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 -version
    javac 1.8.0_25
    
    $ tree 
    .
    ├── classes
    └── src
        └── helloworld
            └── HelloWorld.java
    
    3 directories, 1 file
    
    $ javac -d classes src/helloworld/HelloWorld.java
    
    $ tree
    .
    ├── classes
    │   └── helloworld
    │       ├── HelloWorld$1.class
    │       └── HelloWorld.class
    └── src
        └── helloworld
            └── HelloWorld.java
    
    4 directories, 3 files
    
    $/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/javapackager -createjar -appclass helloworld.HelloWorld -srcdir classes -outdir dist -outfile hello.jar -v
    
    $ tree
    .
    ├── classes
    │   └── helloworld
    │       ├── HelloWorld$1.class
    │       └── HelloWorld.class
    ├── dist
    │   └── hello.jar
    └── src
        └── helloworld
            └── HelloWorld.java
    
    5 directories, 4 files
    
    $ java -jar dist/hello.jar