1

First:

I know, this question was asked about 100 times already. I know, someone already could have gave the right answer.

But anyway, I have to ask this again. I didn't found a solution working for me. sorry.

I'm writing a game in java. Of course I have many packages (folders) with sounds and pictures and so on. But these folders are each of variable size. So I want to save the content of such a folder dynamically in a list.

Usually, I was making this:

File f[] = new File(getClass.getResource("/home/res/").toURI()).listFiles();

Now I can iterate though this file object and save each file. Perfect. Really?

No. When I extract this Project into a jar archive, this fails. All because a uri isn't "hierarchical" or some stuff like this. See this exception:

C:\Users\Administrator\Desktop>java -jar Homework.jar
java.lang.IllegalArgumentException: URI is not hierarchical
        at java.io.File.<init>(Unknown Source)
        at homework.moonface.src.Moonface.loadSounds(Moonface.java:94)
        at homework.moonface.src.Moonface.<init>(Moonface.java:55)
        at control.Overview.main(Overview.java:16)

Ok, I thought, so I need to get this path and add it manual into the file object. (new File("path"); But... this doesn't work. I'm getting the known error that the input wasn't written correctly, or when i try to cut of "file:" from the resource url, it breaks because in a jar its "jar:file:" and not "file:". But also if I cut of jar:file: I'm getting null.

So, please don't mark this as a duplicate, and try to explain this shortly for me. It would help thousand other, who don't understand other solutions who aren't solutions.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
T_01
  • 1,256
  • 3
  • 16
  • 35

1 Answers1

1

Try this :

    URL jarResourceURL = getClass().getResource("/home/res/");
    JarURLConnection jarURLConnection = (JarURLConnection) jarResourceURL.openConnection();
    Enumeration<JarEntry> entries = jarURLConnection.getJarFile().entries();
    while (entries.hasMoreElements()){
        entries.nextElement(); // iterate over entries and do something
    }

UPD: I was thinking about how spring framework's ClassPathXmlApplicationContext resolves the resources from jars. So i investigated the source code and foud that there is an utility class org.springframework.core.io.ClassPathResource which have a convenient interface (moreover there is a possibility to get the corresponding java.io.File instance using it) and can help you to solve the problem. Here is the doc : http://docs.spring.io/spring/docs/2.5.x/api/org/springframework/core/io/ClassPathResource.html

  • 2
    getting this exception: `java.lang.ClassCastException: sun.net.www.protocol.file.FileURLConnection cannot be cast to java.net.JarURLConnection at homework.moonface.src.Moonface.loadSounds(Moonface.java:93) at homework.moonface.src.Moonface.(Moonface.java:55) at control.Overview.main(Overview.java:16)` – T_01 Oct 02 '13 at 07:54
  • Did you tried to work with classpath member which is not in jar ? –  Oct 02 '13 at 08:22
  • not, but the resources have to be in the jar.. or what do you mean? – T_01 Oct 02 '13 at 08:37