5 Replies Latest reply: Nov 6, 2008 6:36 PM by 807589 RSS

    getSystemResource gives me a valid URL but url.openStream throws Exception?


      I'm trying to do something that should be simple, but nevertheless it has me stumped. I want to grab a file that is packaged inside my jar file and write it out to disk. Here's some code:
      URL myUrl = ClassLoader.getSystemResource("META-INF/path/myfile.txt");
      // outputs "jar:file:Volumes/setupMac%201/Inst.app/Contents/Resources/app/user.jar!/META-INF/path/myfile.txt"
          InputStream stream = myUrl.openStream();
      catch (IOException ioe)
          System.out.println(ioe.getMessage()); // outputs "No such file or directory"
      Note that this code works flawlessly on Windows and Linux (RHEL5 and CentOS5) but fails on my mini-mac (Darwin 9.2.2, java version 1.5.0_13). Is there something special I need to do in order to open a jar URL on mac? Or is there something else I'm doing incorrectly?

      By the way, I googled a bit and found a suggestion to use MyClass.class.getClassLoader().getSystemResource() instead of using the system classloader via ClassLoader.getSystemResource(), but I get the same results as above. Also, I have tried using getSystemResourceAsStream, which simply returns null. If anyone has any other suggestions, I am all ears. Thanks.