Jar Path Problem...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • desturrr
    New Member
    • Oct 2009
    • 18

    Jar Path Problem...

    I have written a java application , and i am using sounds , images which are placed in Sounds and images packages. I am having trouble about this paths.

    filePathSounds= getClass().getR esource("/Sounds").toStri ng();

    // i get the substring because there is file:/C:/JavaProjec....
    // i am removing "file:/ "which is 6 characters and getting the proper path
    String pathSounds=file PathSounds.subs tring(6);

    public xPlayer k= new xPlayer(pathSou nds+"/Bird/cikcik.mp3");
    public xPlayer m= new xPlayer(pathSou nds+"/Cat/miaw.mp3");

    Above codes is how i manage to use relative path thing.. This is actually working when i compile and run using Netbeans.

    The Problem is : When i try to make Jar executable file the paths seems don't work i don't get any of my sounds. Basically how should i place the sounds or images so that i can create proper Jar files..

    Thanks in advance.
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    If you use relative paths your sounds and resources must exist relative to the executable. Make sure your sounds are in the relative location to where the executable is running.

    Comment

    • Techninjaz
      New Member
      • Jan 2010
      • 5

      #3
      Go with what RedSon said. Make sure the images and sounds are in the directory that is being compiled to a jar.

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        // i am removing "file:/ "which is 6 characters and getting the proper path
        How do you know the path is correct? Try printing it. In particular if the class is part of a jar archive the resulting string won't make sense either as a file path or a jar: url.

        Comment

        • desturrr
          New Member
          • Oct 2009
          • 18

          #5
          As i mentioned i used breakpoint to see what string is given by "filePathSounds =getClass().get Resource("/Sounds").toStri ng();"
          and it gives an "file:/" thing , it is unnecessary. if i don't delete it i got an error that says "java.io.FileNo tFoundException : file:\C:\Java_P rojects\Ney1\bu ild\classes\Sou nds\Birds\cikci k.mp3"

          i don't know actually when it is a jar file , what is given to that string that i am removing 6 of its first chars...i can not put breakpoint to see what it gives it to me. what to do ?

          ...
          Thanks again.

          Comment

          • pbrockway2
            Recognized Expert New Member
            • Nov 2007
            • 151

            #6
            You can see the result of removing the first 6 characters of the string form of the resource url by printing it. As mentioned if the class is part of a jar archive, treating the url in this way won't result in a valid anything: neither file name nor url.

            My remarks were intended to be othogonal to that of RedSon. You can place the resources somewhere relative to the executable - but only leaves the problem of trying to figure out what the path of that executable is! Getting the getResource() url and dealing with it as if it were a string is more subtle than you seem to imagine and is only going to be effective sometimes.

            Techninjaz suggestion of placing the resources inside the jar archive is better. But note that in this case the resources will not be files (they will be entries inside a jar archive) and therefore will not have file names. If there is a constructor that takes a URL argument, use it. (Or any factory method, or any other way of constructing an xPlayer instance using a url). That way resources within a jar archive will be accessible. Sun's Tutorial chapter on Icons has an example of how to access resources packaged within a jar archive.

            If you must have the resources as files then consider passing the location of the resources as an argument when you launch the application.

            Comment

            Working...