简体   繁体   English

如何使用Gradle访问Java项目中的资源?

[英]How to access a resource in Java project using Gradle?

I have a javafx project I'm building using a Gradle file, and I'm writing everything in Intellij. 我有一个javafx项目,我正在使用Gradle文件构建,我正在Intellij中编写所有内容。 In it, I use javafx.scene.media.Media and javafx.scene.media.MediaPlayer to play some music. 在其中,我使用javafx.scene.media.Media和javafx.scene.media.MediaPlayer播放一些音乐。

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 had been working out of was: 我一直在努力的项目目录是:

src/ SRC /

|---main/ | ---主/

|---|---java/ | --- | ---爪哇/

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

|---|---resources/ | --- | ---资源/

|---|---|---sound/ | --- | --- | ---声音/

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

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

however, when I went to compile and turn it into a jar file, Gradle changed the directory into this (within the jar file, top level): 然而,当我去编译并将其转换为jar文件时,Gradle将目录更改为此(在jar文件中,顶级):

sound/ 声音/

|---SoundPlayer.class | --- SoundPlayer.class

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

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

That made it throw a Media Exception: MEDIA UNAVAILABLE. 这使得它引发媒体异常:MEDIA UNAVAILABLE。 I've 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());

... but I always get the same exception. ......但我总是得到同样的例外。 What's going on? 这是怎么回事?

What Gradle did is completely expected. Gradle做了什么是完全可以预料到的。 The src/main/java and src/main/resources directories store code and resources respectively. src / main / javasrc / main / resources目录分别存储代码和资源。 The resources folder contains all the non-java code like images,sound etc. resources文件夹包含所有非java代码,如图像,声音等。

When creating the jar file, the contents of the resources directory will be copied as is (maintaining the package structure). 创建jar文件时,资源目录的内容将按原样复制(维护包结构)。 Note that click.mp3 and bgm.mp3 are members of the sound package. 请注意, click.mp3bgm.mp3声音包的成员。

So, when you want to load a resource, it should (generally) not be done using file paths. 因此,当您要加载资源时,应该(通常)不使用文件路径来完成。 Instead use, package structure to do so. 而是使用包结构来做到这一点。 Here, as the sounds and SoundPlayer have the same package, ie sound , you can use the SoundPlayer class to load resources like following: 在这里,由于声音和SoundPlayer具有相同的包,即sound ,您可以使用SoundPlayer类加载如下资源:

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

From Javadocs 来自Javadocs

public String toExternalForm() public String toExternalForm()
Constructs a string representation of this URL. 构造此URL的字符串表示形式。 The string is created by calling the toExternalForm method of the stream protocol handler for this object. 通过调用此对象的流协议处理程序的toExternalForm方法来创建该字符串。

In essence, the toExternalForm() function creates the appropriate URL for a given resource. 本质上,toExternalForm()函数为给定资源创建适当的URL。

Here is a complete example. 这是一个完整的例子。

// 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' } } 

and the modified SoundPlayer 和修改后的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();
    }
}

and the Main class to use SoundPlayer 和Main类使用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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM