1

I am trying to follow the second tutorial https://github.com/AlmasB/FXGL/wiki/Adding-Images-and-Sounds-%28FXGL-11%29 and it appears that i get an error when i run the application. I use gradle run or run it in eclipse, without the sound everything works fine.

My project structure looks like this:

enter image description here

I use openJDK 11.0.3 and linux mint 19.1 64-bit.

It is just basically the same program as in the tutorial, i get the following exception:

Message: javafx/scene/media/AudioClip Type: NoClassDefFoundError

Method: DesktopAudioService.loadAudioImpl() Line:

DesktopAudioService.kt:28

My build.gradle is pretty straight forward i guess; the gradle init and the dependencies:

plugins {
    id 'application'
    id 'java-library'
    id 'org.openjfx.javafxplugin' version '0.0.7'
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'

    implementation 'com.google.guava:guava:27.0.1-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    compile 'com.github.almasb:fxgl:11.1-beta'
}

javafx {
    version = "12"
    modules = [ 'javafx.controls' ]
}

mainClassName = 'game.idea.BasicGameApp'

I expect the sound to be played when clicking 'f' on my keyboard without crashing the program. I also hope for some background explanation what causes the error.

madave
  • 97
  • 8

2 Answers2

2

You use the JavaFX Gradle plugin and set the modules in your build like this:

javafx {
    version = "12.0.1"
    modules = [ 'javafx.controls' ]
}

This means that the plugin will add to your project the javafx.base, javafx.graphics and javafx.controls modules, with the version and correct classifier based on your platform.

If you check your external libraries, you won't find any other JavaFX module implementation, but you might find the "empty" modules that FXGL is using:

So Base, Graphics and Controls use the Mac classifier (in my case), and the version I've set (12.0.1), while the other modules (FXML, Media and Swing) are empty modules added from FXGL (see for instance the Media dependency).

When you run your project, the Media classes are not there, so when you try to play the sound you get the reported exception:

Fatal exception occurred: java.lang.NoClassDefFoundError : javafx/scene/media/AudioClip E: com.almasb.fxgl.audio.impl.DesktopAudioService.loadAudioImpl(DesktopAudioService.kt:28) E: com.almasb.fxgl.audio.impl.DefaultAudioService.loadAudio(DefaultAudioService.kt:29) E: com.almasb.fxgl.app.AssetLoader.loadSound(AssetLoader.kt:247) E: com.almasb.fxgl.dsl.FXGL$Companion.play(FXGL.kt:228) E: com.almasb.fxgl.dsl.FXGL.play(FXGL.kt) E: game.idea.BasicGameApp$5.onActionBegin(BasicGameApp.java:61)

The solution is quite easy: just add the missing modules to your build:

javafx {
    version = "12.0.1"
    modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.swing', 'javafx.media' ]
}

Finally, as an aside, you can use 'com.github.almasb:fxgl:11.3'.

José Pereda
  • 44,311
  • 7
  • 104
  • 132
0

If you are using OpenJDK , then JavaFx might not be available. This will be the reason for the exception. Change to Oracle JDK if you are using windows. On linux, there are other ways with OpenJDK itself.

Refer to this post JavaFX and OpenJDK for some details on how-to.

Kris
  • 8,680
  • 4
  • 39
  • 67
  • Thank you for your answer. I hope this is not the problem. I gonna check this these days, but oraclejdk seems not as easily installed as before anymore. – madave Jul 08 '19 at 15:52