Can't find resources in my fatjar

I am building an executable .jar file with FatJar, except I am running into a problem.



Inside of Eclipse,


        machine_gunSound = audio.createAudioTrack(theingame.class.getClassLoader().getResource(
           "MouseWork/audio/gun1.ogg/"), false);



works just fine, but from within my executable .jar file, it does not see the sound. The sound is being included in the .jar file, so what is the correct way to access it from inside of the .jar?

I had the same problem. As far as I still know there were 2 options to solve the problem.

  1. avoid the fat.jar and use singles jars.
  2. Take your resources out of the fat.jar ,place it in a folder somewhere and add it in the classpath (a resource.jar might work as well)



    Hope that works.


Hey thanks for the pointers.



However, I am going to need help to do this, because this is the part of java I am the worst at right now.



Ok, so I want to do option 2 (basically, just take all my resources and put them in a Resources.jar)



My question now is, with code how exactly would I access these outside resources from inside my fatjar .jar file?

Ok, I tested it with eclipse 3.5. Most of the files were loaded from within the jar but I had one problem-file.

With extending it to another jar didn't work (n)either.  Only thing that helped was copying the whole package in the directory where my fat.jar was and including the current directory to the classpath. Like this:


java -Djava.library.path=E:windows_workspacejme2liblwjglnativewindows -classpath . -jar test.jar



Actually that made me curious and I had a deeper look into that. So it seems that filenames in jar-archives are casesensitive and using the resourceloader not. So maybe checking all filesnames can help here also....

After I changed the filename even the fatjar works...

Hi, I am not home yet to test my filenames (by the way thanks for all the help with this) but there is one thing I want to know.



How are you loading your resources? I know these loaders are picky with the "/"



Also, where do you include your resources relative to your source folder?



I am just wondering, thanks. Also I will post again when I get home and try out the names of the files, to let you know how that goes.



//edit:



I have been looking around on the forum and I am becomming more and more confused. There seems to be different ways to load a resource, the way I showed above as well as ResourceLocaterTool. According to one post on this forum, the ResourceLocaterTool is the way to get a resource from inside a jar file.



I am just curious ttrocha about how you, in your tests, loaded your resources with code.

I load it the way you do!

thanks for all the help ttrocha, after toying with it for a while, these ways work for both inside eclipse and in the .jar



for audio:

        hitSound = audio.createAudioTrack(theingame.class.getClassLoader().getResource(
                "MouseWork/audio/crate_collectan.ogg"), false);



for .mesh.xml and textures


      Node model;
        OgreLoader loader = new OgreLoader();
        MaterialLoader matLoader = new MaterialLoader();
        String matUrlString = "/MouseWork/bullet/Scene.material";
        String ninjaMeshUrlString =
                "/MouseWork/bullet/Cylinder02_Material01.mesh.xml";

        try {
            URL matURL = ResourceLocatorTool.locateResource(
                    ResourceLocatorTool.TYPE_TEXTURE, matUrlString);
            URL meshURL = ResourceLocatorTool.locateResource(
                    ResourceLocatorTool.TYPE_MODEL, ninjaMeshUrlString);

            if (meshURL == null)
                throw new IllegalStateException(
                        "Required runtime resource missing: "
                        + ninjaMeshUrlString);
            if (matURL == null)
                throw new IllegalStateException(
                        "Required runtime resource missing: " + matUrlString);
            try {
                ResourceLocatorTool.addResourceLocator(
                        ResourceLocatorTool.TYPE_TEXTURE,
                        new RelativeResourceLocator(matURL));
                  // This causes relative references in the .material file to
                  // resolve to the same dir as the material file.
                  // Don't have to set up a relative locator for TYPE_MODEL
                  // here, because OgreLoader.loadModel() takes care of that.
            } catch (URISyntaxException use) {
                // Since we're generating the URI from a URL we know to be
                // good, we won't get here.  This is just to satisfy the
                // compiler.
                throw new RuntimeException(use);
            }
            matLoader.load(matURL.openStream());
            if (matLoader.getMaterials().size() > 0)
                loader.setMaterials(matLoader.getMaterials());

            model = (Node) loader.loadModel(meshURL);
            scene.attachChild(model);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (ModelFormatException mfe) {
            logger.log(Level.SEVERE, null, mfe);
        }
        scene.setLocalScale(.02f);



for .3ds

    public Node buildLevel() throws IOException{
       Node main = new Node("main");       
      Node model = null;
        MaxToJme loader = new MaxToJme();
        String matUrlString = "/MouseWork/level/level.jpg";
        String ninjaMeshUrlString =
                "/MouseWork/level/room2.3DS";

        URL matURL = ResourceLocatorTool.locateResource(
              ResourceLocatorTool.TYPE_TEXTURE, matUrlString);
      URL meshURL = ResourceLocatorTool.locateResource(
              ResourceLocatorTool.TYPE_MODEL, ninjaMeshUrlString);

      if (meshURL == null)
          throw new IllegalStateException(
                  "Required runtime resource missing: "
                  + ninjaMeshUrlString);
      if (matURL == null)
          throw new IllegalStateException(
                  "Required runtime resource missing: " + matUrlString);
      try {
          ResourceLocatorTool.addResourceLocator(
                  ResourceLocatorTool.TYPE_TEXTURE,
                  new RelativeResourceLocator(matURL));
      } catch (URISyntaxException use) {
          throw new RuntimeException(use);
      }
      ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
      URL url = null;            
      InputStream is=null;
      try {
          is=meshURL.openStream();
      } catch(Exception err)  {
          System.out.println("Could not open Model file: "+err);
      }
      try {
          loader.convert(is, bytearrayoutputstream);
          BinaryImporter binaryImporter = new BinaryImporter();
          ByteArrayInputStream in=new ByteArrayInputStream(bytearrayoutputstream.toByteArray());
          model = (Node)binaryImporter.load(in);
      } catch(Exception err)  {
          System.out.println("Error loading md3 model:"+err);
      }

      main.attachChild(model);     
      return main;
       
    }