尝试使用 Media Player 时 Application Constructor 出现异常

Exception in Application Constructor while trying to use Media Player

我写了一个代码,当在网页上找到 link 时会播放音乐。

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javafx.application.*;
// * @author Archit

public abstract class WebCrawl extends Application{

    public static void main(String[] args) throws IOException {
        Application.launch(args);
        int a=0;
        try {
            Document doc = Jsoup.connect("https://in.bookmyshow.com/ranchi").get();
            org.jsoup.select.Elements links = doc.select("a");
            for (Element e: links) {
                if ((e.attr("abs:href").equals("https://in.bookmyshow.com/ranchi/movies/fan/ET00025074"))) {
                    try {
                        File f = new File("/Users/Archit/Documents/Music/campbell.wav");
                        Media hit = new Media(f.toURI().toString());
                        MediaPlayer mediaPlayer = new MediaPlayer(hit);
                        mediaPlayer.play();
                    } catch(Exception ex) {
                        System.out.println("Exception");
                    }
                }
            }
        }

我得到的错误是:

Exception in Application constructor java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at       sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application   instance: class webcrawl.WebCrawl
    at   com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication5(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.InstantiationException
    at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null3(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater4(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application webcrawl.WebCrawl

我运行应用程序时似乎打开了一个window,但自动关闭并出现此错误。

非常感谢您的帮助。谢谢。

您正在使用 Application.launch(String[])WebCrawl class 启动一个应用程序,因此 launch 尝试创建一个 WebCrawl 实例,它可以' t,因为 WebCrawlabstract.

顺便说一句:在 Application.launch 调用之后放置代码是行不通的,因为在 Application.launch 完成后,JavaFX 平台将已经退出。

您可以在 Life-cycle section of the Application javadoc 中阅读有关应用程序生命周期的信息。

您需要从 start 方法或更高版本调用代码。

您可以在此处找到简单 JavaFX 应用程序的教程:https://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm