How to access a resource in a Java project using Gradle?

I have a javafx project that I create using a Gradle file and I write everything in Intellij. In it, I use javafx.scene.media.Media and javafx.scene.media.MediaPlayer to play music.

public SoundPlayer(String filename) {
    String soundLocation = "\\src\\main\\resources\\sound\\" + fileName;
    String absolute = new File("").getAbsolutePath() + soundLocation;
    System.out.println(absolute);
    Media soundMedia = new Media(new File(absolute).toURI().toString());
    mediaPlayer = new MediaPlayer(soundMedia);
}

The project directory I was working on was:

CSI /

| --- home /

| --- | --- Java /

| --- | --- | --- sound.SoundPlayer

| --- | --- resources /

| --- | --- | --- sound /

| --- | --- | --- | --- click.mp3

| --- | --- | --- | --- bgm.mp3

however, when I went to compile and turned it into a jar file, Gradle changed the directory to this (in the jar file, top level):

sound/

| --- SoundPlayer.class

| --- click.mp3

| --- bgm.mp3

This made him throw a media exception: MEDIA CONTINUOUSLY. I tried changing the file to both of the following:

Media soundMedia = new Media(new File("sound\\" + fileName).toURI().toString());

and

Media soundMedia = new Media(new File(fileName).toURI().toString());

... . ?

+4
1

Gradle. src/main/java src/main/resources . -java-, , ..

jar ( ). , click.mp3 bgm.mp3 .

, , ( ) . . , SoundPlayer , .. sound, SoundPlayer , :

public SoundPlayer(String filename) {
    URL resource = SoundPlayer.class.getResource(filename);
    Media soundMedia = new Media(resource.toExternalForm());
    mediaPlayer = new MediaPlayer(soundMedia);
}

Javadocs

public String toExternalForm()
URL-. toExternalForm .

, toExternalForm() URL .

.

// build.gradle
apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'


mainClassName = 'sound.Main'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.10'
}

jar { manifest { attributes 'Main-Class': 'sound.Main' } } 

SoundPlayer

//sound.SoundPlayer
package sound;

import java.net.URL;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;

public class SoundPlayer {

    private MediaPlayer mediaPlayer;

    public SoundPlayer(String filename) {
        URL resource = SoundPlayer.class.getResource(filename);
        Media soundMedia = new Media(resource.toExternalForm());
        mediaPlayer = new MediaPlayer(soundMedia);
    }

    public void play(){
        mediaPlayer.play();
    }
}

SoundPlayer

// sound.Main
// This class does not actually create a JavaFX UI. Instead, it is
// only creating a JavaFX application to use Media
package sound;

import javafx.application.Application;
import javafx.stage.Stage;

/**
 *
 * @author aga53
 */
public class Main extends Application{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        SoundPlayer s = new SoundPlayer("test.mp3");
        System.out.println("Hello World");
        s.play();
    }

}
+8

Source: https://habr.com/ru/post/1612972/


All Articles