1

Fairly new to Java and trying to teach myself a little about JavaFX. I'm trying to create a simple JavaFX video/media player that runs when I click a video file. I want to create the actual player as a separate class that accepts the video file location as an a string argument.

When I run the below,

public class BLPlayer{
    public static void main(String[] args) {
        if(args.length > 0){
            VideoPlayer vp = new VideoPlayer(args);
        }else{
            //showGUI();
        }
    }
}


public class VideoPlayer extends Application {
    String path;
    MediaPlayer player;
    Scene scene;
    MediaView view;
    Group root;
    Media media;

    VideoPlayer(String[] args){
        path = args[0];
        path = path.replace("\\", "/"); 
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception {

        File f = new File(path);

        root = new Group();
        media = new Media(f.toURI().toString());
        player = new MediaPlayer(media);
        view = new MediaView(player);
        root.getChildren().add(view);
        scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        player.play();
    }

}

I get the error:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class player.VideoPlayer
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:884)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: player.VideoPlayer.<init>()
at java.lang.Class.getConstructor0(Class.java:2971)
at java.lang.Class.getConstructor(Class.java:1812)
at com.sun.javafx.application.LauncherImpl$7.run(LauncherImpl.java:790)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)

I don't know why it isn't working. I'd appreciate any tips or advice! Thanks for any help.

Tony
  • 51
  • 1
  • 5

1 Answers1

0

The error occurs because you need to launch the JavaFX application using the launch(). For more details go through this solution.

If you really need to send parameters to the VideoPlayer class. You can fetch the parameters using the getParameters() of the Application class.

public class VideoPlayer extends Application {
    String path;
    MediaPlayer player;
    Scene scene;
    MediaView view;
    Group root;
    Media media;

    @Override
    public void start(final Stage stage) throws Exception {
        Parameters params = getParameters();
        final List<String> parameters = params.getRaw();
        path = !parameters.isEmpty() ? parameters.get(0) : "";
        path = path.replace("\\", "/"); 
        root = new Group();
        File f = new File(path);
        root = new Group();
        media = new Media(f.toURI().toString());
        player = new MediaPlayer(media);
        view = new MediaView(player);
        root.getChildren().add(view);
        scene = new Scene(root, 400, 400, Color.BLACK);
        stage.setScene(scene);
        stage.show();
        player.play();
    }
}

To inititate the JavaFX application from another class

public class BLPlayer {
    public static void main(String[] args) {
        if(args.length > 0){
            Application.launch(VideoPlayer.class, args);
        }
    }
}
Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176