Javafx 2 JAR文件中的JavaFX媒体\u不可用
我目前正试图让JavaFX媒体播放器正常工作,在打包应用程序时,在查找媒体文件时出现了一些奇怪的行为。在eclipse中运行它时,它工作得很好,但当我用maven将其打包为一个jar时,媒体文件就无法再找到,我得到以下错误:Javafx 2 JAR文件中的JavaFX媒体\u不可用,javafx-2,javafx,executable-jar,Javafx 2,Javafx,Executable Jar,我目前正试图让JavaFX媒体播放器正常工作,在打包应用程序时,在查找媒体文件时出现了一些奇怪的行为。在eclipse中运行它时,它工作得很好,但当我用maven将其打包为一个jar时,媒体文件就无法再找到,我得到以下错误: Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.simontuffs.onejar.Boot.run(Boot.java:340)
at com.simontuffs.onejar.Boot.main(Boot.java:166)
Caused by: MediaException: MEDIA_UNAVAILABLE : \resources-0.0.1-SNAPSHOT.one-jar.jar
(Das System kann die angegebene Datei nicht finden)
at javafx.scene.media.AudioClip.<init>(AudioClip.java:65)
at com.example.test.MyResourceTest.getResource(MyResourceTest.java:11)
at com.example.test.MyResourceTest.main(MyResourceTest.java:18)
... 6 more
事实上,我刚刚找到了一个很好的方法来解决这个问题 使用: 在应用程序类中获取documentbase, 然后使用类似
Media media = new Media(documentBase + "/resources/" + name + ".mp3");
player = new MediaPlayer(media);
或者在您的情况下:
Media media = new Media(documentBase + "/com/example/data/sound.mp3");
player = new MediaPlayer(media);
(这仅在使用javafx应用程序时有效)事实上,我刚刚找到了一个非常好的方法来解决这个问题 使用: 在应用程序类中获取documentbase, 然后使用类似
Media media = new Media(documentBase + "/resources/" + name + ".mp3");
player = new MediaPlayer(media);
或者在您的情况下:
Media media = new Media(documentBase + "/com/example/data/sound.mp3");
player = new MediaPlayer(media);
(这仅适用于使用javafx应用程序的情况)