Results 1 to 18 of 18
  1. #1
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Accessing resource files within JAR

    I'm trying to include a folder of MP3s inside my JAR for access on the web. As per the NetBeans instructions, I put the folder in the same directory as the .java files so that the MP3s are in the folder notes and md is the package: src/md/notes/. When I compile after adding the MP3s, the JAR increases in size by approximately the total sizes of all the MP3s. My code requires a file object and not an input stream. Google searches show that I should be able to access the file with

    this.getClass().getResource(String filePath)
    or
    this.getClass().getClassLoader().getResource(Strin g filePath)

    I can access MP3s outside the jar with absolute filePaths like "/notes/0.mp3". When I use relative filePaths like "notes/0.mp3" I get a null pointer exception

    javax.media.NoPlayerException: Cannot find a Player for :jar:file:/C:/dist/MD.jar!/md/notes/0.mp3

    If the file location is right, the problem might be that getResource is returning it in the wrong format for the MediaLocator(File file) I am using to open it. I know the rest of the code is correct because everything works perfectly when I use an absolute path to access the files outside the JAR.

    Please help.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Accessing resource files within JAR

    Resources inside a jar are not files.
    My code requires a file object and not an input stream.
    Can it be reworked to take a URL?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Accessing resource files within JAR

    My code requires a file object
    Then your code must change.

    getResource() - either flavour - returns an instance of URL. Only if the url concerned is a file: url do you have any hope of creating an instance of File. This won't be the case if your mp3 is an entry within a jar archive where you will get a jar: url similar to the one in the NoPlayerException error message.

    It is important to realise that once you put the mp3 into a jar archive you no longer have an .mp3 file to access. What you have is a .jar file. And if your aim (as you state) is to access the mp3 from the web somewhere then you don't even have a .jar file. You have URLs and the InputStream instances you can create from them.

  4. #4
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    Darryl, from what I understand from pbrockway2, the url cannot refer to a file inside the JAR.

    Perhaps there's an easier way. This is what I was doing:

    Player player = null;
    player = Manager.createPlayer(new MediaLocator(this.getClass().getResource(notePath) ));

    createPlayer can take a DataSource, MediaLocator, or URL.

    The URL and MediaLocator won't do me any good (MediaLocator can take an URL or String). This leaves DataSource. Is there any way to get a DataSource from the MP3s in the JAR? I don't understand how the DataSource interface works.

    If I'm headed down a path that is beyond me, I can make do with the MP3s outside the JAR.

    Thanks,
    D

  5. #5
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    Thanks to your responses. You're right in that what I really needed was the url of the files. I've read everything I could find about this, and I am unable to get it working. I would appreciate it if I can get some more help. No matter what I do, I can't get the url to my files in the jar.

    I can get the audio running from NetBeans because NetBeans automatically copies the audio file into the build directory.

    URL url = getClass().getResource("0.mp3");
    System.out.print(url.toString());

    file:/C:/My%20Documents/NetBeansProjects/Test/build/classes/test/0.mp3

    From the command prompt, I get:

    URL url = getClass().getResource("0.mp3");
    System.out.print(url.toString());

    jar:file:/C:Test.jar!/test/0.mp3

    where test is the package. When I use the url, I get a null pointer exception.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Accessing resource files within JAR

    Make sure the 0.mp3 entry really is inside the jar archive where it should be. (One way to check is to rename the .jar archive so it ends ".zip" then it can be opened as any other .zip archive. Alternatively the jar executable enables the contents to be extracted.)

    A small gotcha when working with Windows is that file names are *not* case sensitive, but entries in a .jar archive are. So if the file on disk is 0.MP3 then the file will be found within the build directory when you refer to it as "0.mp3", but the corresponding entry inside the .jar archive will not be found because MP3!=mp3.

    -----

    If the entry does exist, post a small compilable example that shows the problem. And say what the relevant directory structure of the source files is.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Accessing resource files within JAR

    What folder is the file in inside of the jar file?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    Thanks for the help. Test.java and 0.mp3 are NetBeansProjects\Test\src\test

    Test.jar renamed to Test.zip has two folders in it: META-INF and test. The latter has two files: Test.class and 0.mp3.

    The following programs runs correctly in NetBeans, but not from the command prompt.

    Java Code:
    package test;
    
    import java.io.IOException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.media.*;
    import javax.media.format.AudioFormat;
    
    public class Test {
        
        public void Test() {
        }
    
        public static void main(String[] args) {        
            Test test = new Test();
            test.playMP3();
        }
        
        public void playMP3() {
            URL url = getClass().getResource("0.mp3");
            System.out.print(url.toString());        
            /**
            * Thanks to Joemoe for the following PlayMP3 code
            * [url]http://www.morgenstille.at/blog/how-to-play-a-mp3-file-in-java-simple-and-beautiful/[/url]
            */                
            Format input1 = new AudioFormat(AudioFormat.MPEGLAYER3);
            Format input2 = new AudioFormat(AudioFormat.MPEG);
            Format output = new AudioFormat(AudioFormat.LINEAR);
            PlugInManager.addPlugIn(
                    "com.sun.media.codec.audio.mp3.JavaDecoder",
                    new Format[]{input1, input2},
                    new Format[]{output},
                    PlugInManager.CODEC);        
            Player player = null;
            try {
                player = Manager.createPlayer(new MediaLocator(url));
            } catch (IOException | NoPlayerException ex) {
                Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
            }
            player.start();
        }
    }
    Last edited by pbrockway2; 06-18-2012 at 04:33 AM. Reason: code tags added

  9. #9
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    p.s. Sorry that the indents didn't post.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Accessing resource files within JAR

    The following programs runs correctly in NetBeans, but not from the command prompt.
    Please post the full contents of the command prompt window.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Accessing resource files within JAR

    Quote Originally Posted by Dissonance View Post
    p.s. Sorry that the indents didn't post.
    BB Code List - Java Programming Forum

  12. #12
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    C:\>java -jar Test.jar
    jar:file:/C:/Test.jar!/test/0.mp3Jun 17, 2012 2:12:39 PM test.Test playMP3
    SEVERE: null
    javax.media.NoPlayerException: Cannot find a Player for :jar:file:/C:/Test.jar!/
    test/0.mp3
    at javax.media.Manager.createPlayerForContent(Manager .java:1412)
    at javax.media.Manager.createPlayer(Manager.java:417)
    at test.Test.playMP3(Test.java:37)
    at test.Test.main(Test.java:17)

    Exception in thread "main" java.lang.NullPointerException
    at test.Test.playMP3(Test.java:41)
    at test.Test.main(Test.java:17)

    C:\>

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,271
    Rep Power
    25

    Default Re: Accessing resource files within JAR

    javax.media.NoPlayerException: Cannot find a Player for
    The program is starting execution and finding the file but the jre is missing some parts?
    Are there different versions of java being used?
    Last edited by Norm; 06-18-2012 at 08:54 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Accessing resource files within JAR

    What does the sysout of url show? Is it the same in the case of running from the IDE and from the .jar?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    Quote Originally Posted by DarrylBurke View Post
    What does the sysout of url show? Is it the same in the case of running from the IDE and from the .jar?

    db
    From the IDE: file:/C:/My%20Documents/NetBeansProjects/Test/build/classes/test/0.mp3
    plays ok
    From the command prompt: jar:file:/C:Test.jar!/test/0.mp3
    gives the exception

  16. #16
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    Quote Originally Posted by Norm View Post
    The program is starting execution and finding the file but the jre is missing some parts?
    Are there different versions of java being used?
    I uninstalled everything Java, installed the latest NetBeans with latest JDK and JMF, and verified the classpath is correct. No change.

    I read everything I could find on "javax.media.NoPlayerException: Cannot find a Player". I wonder if the player is having a problem constructing because of the media type. Also, based on what I read on google, I wonder if there's a problem with JMF.

    There's a library called JLayer that plays mp3s. I'll give that a try.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Accessing resource files within JAR

    Quote Originally Posted by Dissonance View Post
    From the IDE: file:/C:/My%20Documents/NetBeansProjects/Test/build/classes/test/0.mp3
    plays ok
    From the command prompt: jar:file:/C:Test.jar!/test/0.mp3
    gives the exception
    You can write code that extracts the JarEntry, writes its content to a temp file which is deleted when the program exits, and you can play the mp3 from that temp file. See the API for java.io.File for the methods you'll need. Oh, and the JarFile/JarEntry etc. APIs too.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Dissonance is offline Programming Again
    Join Date
    May 2012
    Location
    Eugene, OR
    Posts
    55
    Rep Power
    0

    Default Re: Accessing resource files within JAR

    I got everything working using JLayer. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. Runnable jar does not include my resource files
    By owboateng in forum Eclipse
    Replies: 2
    Last Post: 05-09-2011, 01:54 PM
  3. Accessing metadata of files
    By LianaN in forum New To Java
    Replies: 11
    Last Post: 09-19-2010, 05:49 PM
  4. Randomly accessing files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-12-2007, 10:46 AM
  5. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •