4

This is related to my previous question in that it is the same scenario, just one step further.

I have a testing class called Sample which is a simple wrapper for a Wave object from the musicg library:

import com.musicg.wave.Wave;

public class Sample {

    private String filename;
    private Wave wave;
    private WaveHeader waveheader;

    public Sample(String filename) {
        this.filename = filename;
        wave = new Wave(this.filename);
        waveheader = wave.getWaveHeader();
    }
}

I am trying to initialize a Sample object by passing an absolute path to the constructor. This works within Eclipse. But once I export a runnable Jar file, I get a java.io.FileNotFoundException upon passing a path to a .wav file that is included in the Jar file. Here's what I get:

java.io.FileNotFoundException: /Users/stas/Documents/java/workspace/EclipseTest/EclipseTest.jar/ORGCSDH1.wav (Not a directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.io.FileInputStream.<init>(FileInputStream.java:79)
    at com.musicg.wave.Wave.<init>(Wave.java:60)
    at stm.vst.test.Sample.<init>(Sample.java:14)
    at stm.vst.test.Test.populateSamples(Test.java:70)
    at stm.vst.test.Test.main(Test.java:14)

What I am stumped by is the (Not a directory) part. This is not very helpful, as obviously it shouldn't be a directory, either. This is different from a File not Found exception. The file is verifiably there.

I should remark that I am new to runnable Jar files, so I am sure there is something very simple I am missing.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
principal
  • 493
  • 1
  • 6
  • 15
  • 2
    because the following is not a directory: `/Users/stas/Documents/java/workspace/EclipseTest/EclipseTest.jar/` – Taylor Feb 19 '14 at 16:03
  • `EclipseTest.jar` is probably a jar file rather than a directory so you can't include it in a file path like that. – greg-449 Feb 19 '14 at 16:04
  • 1
    You must pass a relative path – Crickcoder Feb 19 '14 at 16:05
  • RIGHT. I knew it was something extremely simple. So the approach of passing an absolute path to the constructor is flawed to begin with, and I should be passing a relative path based on where the class file resides, from what I understand? Apologies if my terminology is off. – principal Feb 19 '14 at 16:07
  • Just pass the relative path /ORGCSDH1.wav, it should result in /Users/stas/Documents/java/workspace/EclipseTest/ORGCSDH1.wav (where the *.jar is) – Arthur Rey Feb 19 '14 at 16:13

1 Answers1

0

When resources are packed into jar files they are not available as file on HDD but only as resources; follow Java Images not drawn when running java -jar as well as How do I convert a "jar:file" URI to the path of Jar file?

Community
  • 1
  • 1
Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69