title: How to access a resource in Java project using Gradle?
body: <p>I have a javafx project I'm building using a Gradle file, and I'm writing everything in Intellij. In it, I use javafx.scene.media.Media and javafx.scene.media.MediaPlayer to play some music.</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>The project directory I had been working out of was:</p>
  
  <p>src/</p>
  
  <p>|---main/</p>
  
  <p>|---|---java/</p>
  
  <p>|---|---|---sound.SoundPlayer</p>
  
  <p>|---|---resources/</p>
  
  <p>|---|---|---sound/</p>
  
  <p>|---|---|---|---click.mp3</p>
  
  <p>|---|---|---|---bgm.mp3</p>
  
  <p>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):</p>
  
  <p>sound/</p>
  
  <p>|---SoundPlayer.class</p>
  
  <p>|---click.mp3</p>
  
  <p>|---bgm.mp3</p>
  
  <p>That made it throw a Media Exception: MEDIA UNAVAILABLE. I've tried changing the file to both of the following:</p>
  
  <pre><code>Media soundMedia = new Media(new File("sound\\" + fileName).toURI().toString());
  </code></pre>
  
  <p>and</p>
  
  <pre><code>Media soundMedia = new Media(new File(fileName).toURI().toString());
  </code></pre>
  
  <p>... but I always get the same exception. What's going on?</p>