1

I know this question has been asked a bunch of times before, but I'm peeled through all the other threads and tried a bunch of stuff, but can't find anything that resolves my issue. I have a program that compiles and runs without issue in Eclipse, but when I export a runnable .jar file, it won't launch. I tried running it from the cmd prompt, and got the error Illegal Argument Exception: URI in not hierarchical. This is happening in an included sound file which I have as a classpath resource. The code is like this:

try {
    pop = new File(IntroView.class.getResource("/model/pop.wav")
                .toURI());
} catch (URISyntaxException e1) {
     // TODO Auto-generated catch block
    e1.printStackTrace();
}

From what I've read it's a problem with the way that the file is being packed up into the .jar, but I'm having a hard time wrapping my head around it. Can anybody shed some light on this and possibly provide a solution? Thanks.

mkobit
  • 43,979
  • 12
  • 156
  • 150
user3226170
  • 175
  • 1
  • 1
  • 14
  • see [Java Jar file: use resource errors: URI is not hierarchical](http://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical) – Dongqing Jan 14 '15 at 06:23
  • I looked at those, and seeing as I require a file I would have to use the `Bundle` class. I looked around for the library to download so that I can use it, but couldn't find what I needed (or what I thought I needed anyway...). – user3226170 Jan 14 '15 at 06:46

2 Answers2

0

I am sorry but it seems you cannot represent a File object from inside a JAR. When locating a file using File object it checks for file in the OS directory structure only. The File object can locate the JAR itself in a directory but not what's inside.

You can get the InputStream to the file inside JAR like this as stated in a few places:

InputStream input = PlaySound.class.getResourceAsStream("Kalimba.mp3");

You could have these options:

  1. Read the file from JAR and write it outside in your directory and then get the File Object.
  2. Extract the JAR to a folder and point to that with a File.
  3. Simply get the InputStream and play the file as shown here: How can I play sound in Java?
Community
  • 1
  • 1
ZakiMak
  • 2,072
  • 2
  • 17
  • 26
  • I gave that a shot but it's giving me a `java.io.FileNotFound` exception, giving the path then `(The filename, directory name, or volume label syntax is incorrect)`. That was right in Eclipse too, not even in the runnable .jar yet... – user3226170 Jan 14 '15 at 07:08
0

Alright so I got it working, but it's not really an ideal solution. What I ended up doing is creating a folder within the project, but outside of the source. So before, the resources were in

Project/src/Model/pop.wav

Now they are in

Project/Resources/pop.wav

I then just accessed then like this

pop = new File("Resources/pop.wav");

So as this stands, it still only works when launching from the IDE, but what I did was add a new folder within the same folder that the .jar is being run from which contained all the same resource files. The file reference looks for pop.wav relative to whichever directory the program(either in the IDE of from the .jar) is being run from, so it finds the files in this new folder and works fine. I don't feel it's the prettiest solution, but it works anyway.

user3226170
  • 175
  • 1
  • 1
  • 14