Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with accessing resources in JAR

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Post Problem with accessing resources in JAR

    Hi, I'm making a test program for resource loading inside a jar and I'm having problems.

    I've read that you should use ClassLoader or alternatively getClass() to access a file inside a jar. Since the method that i use to load the resource is static, I use ClassLoader.getSystemResource(String path).
    My program finds the file but says that the path contains invalid syntax for filenames or directory names. This is the code that I use to load my resources:

    String path = ClassLoader.getSystemResource(file).getPath();
     
    			System.out.println(path);
     
    			try {
     
    				wave = WaveData.create(new BufferedInputStream(new FileInputStream(path)));
     
    			} catch (FileNotFoundException e) {
     
    				e.printStackTrace();
    			}
    			AL10.alBufferData(buffers.get(i), wave.format, wave.data, wave.samplerate);
    			wave.dispose();s

    Yes, I'm loading soundfiles for OpenAL.

    Anyway, I should remind you that it actually finds the file but the path has invalid syntaxing. This is an example of the path I get when I run it in jar form:

    file:\C:\Users\name\development\jars\test.jar!\sou nds\test.wav

    I've tried to remove the "!" and I still get the same error. If i remove the "file:" it doesn't find the file. Note that the program runs fine when I'm running in Eclipse.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Problem with accessing resources in JAR

    Running fine in Eclipse is expected, since it is a closed environment and doesn't operate from a jar.
    What is the value of: file?
    Have you tried to get the resource with the ClassLoader's getResourceAsStream() method?
    It doesn't matter if the method is static, you can get the Class Object by saying: foo.class
    And then you can get the ClassLoader from the Class Object like you would with the getClass() method (I think, I haven't tried).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Problem with accessing resources in JAR

    Quote Originally Posted by Dr.Code View Post
    String path = ClassLoader.getSystemResource(file).getPath();
    That getSystemResource returns an URL. Don't try to get path, remove parts or other tricks. An URL contains a resource specification in a particular format (especially for resources in jars!) and should be used only with APIs that deals correctly with URL objects.

    From ClassLoader use:
    public static InputStream getSystemResourceAsStream(String name)

    which gives an InputStream you can use instead of any trick you was trying with FileInputStream.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. [SOLVED] Loading Resources: Maven Project Structure vs Jar
    By aussiemcgr in forum Java IDEs
    Replies: 13
    Last Post: August 12th, 2014, 06:40 AM
  2. Loading Resources: Maven Project Structure vs Jar
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 1st, 2013, 10:13 PM
  3. accessing class method problem
    By steel55677 in forum Object Oriented Programming
    Replies: 11
    Last Post: February 23rd, 2012, 08:50 PM
  4. Accessing an external JAR file using Applet
    By rameshiit19 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 28th, 2011, 12:51 AM
  5. Access jar's resources within a servlet
    By RogerBotter in forum Java Servlet
    Replies: 2
    Last Post: July 14th, 2010, 10:18 AM

Tags for this Thread