jaring freakin sucks

Here’s the deal.

Jaring works when all your content is in a class file.

Classes inside Jar files cannot read data that isn’t from a Class file also inside the Jar. IE: pics, files.
I had to get my friend to download the external data files seperately. It worked then.

Why can’t programs read image/data files while in the Jar?

I have done everything correctly.

http://members.optusnet.com.au/ksaho/Algorithm/JAVA/prototypes/neoRpg.jar

??

You CAN read everything from a jar. Just the correct URL is necessary. Which one do you use?

Works fine for me, I do it all the time. Usually using getResourceAsStream.

In fact my app has some graphics in a different jar, just so my app can be easily branded with different splash screens etc. by replacing that one jar file. It all works without a hitch.

I think that’s why it never worked.

What would be the best way to do so?
Would it be better just for people to extract the data files to a specific path and run the Jar as is?

[quote]I think that’s why it never worked.

What would be the best way to do so?
Would it be better just for people to extract the data files to a specific path and run the Jar as is?
[/quote]
Huh? What are you talking about? What are you referring to with “why it never worked”?
We are saying you don’t need to extract the data files.


import java.io.*;
import java.net.*;
import javax.imageio.*;
import java.awt.image.*;

public class ImageLoader
{
   public static BufferedImage load(String name)
   {
      BufferedImage image;
      try
      {
         URL imageUrl = ImageLoader.class.getResource(name);
         image = ImageIO.read(imageUrl);
      }
      catch(IOException ioe)
      {
         GUI.dm.printErr("couldn't load: "+name);
         image=null;
      }
     return image;
   }
}

BufferedImage img = ImageLoader.load("/gfx/shadow.png");

or

this.setIconImage(ImageLoader.load(“icon.gif”));

etc

Pretty easy huh?

The data files need to be there because that’s where all the content comes from.
Without it I have an empty program.

I’m a little dissapointed that I need to use “getResource” to get text files and pictures to be read from within the Jar file.

Thanks fellas.

Ok smart guys, so how do you access a jar from an applet?

?
You don’t access the “jar”, the VM just has a ClassLoader which has a Bunch of Stuff available to it, fed to it by various means like jars, either locally or remotely. The way you access resources on the classpath is the same no matter how the code is deployed, that is, you use

someInstance.getClass().getResource("...");

or

someInstance.getClass().getResourceAsStream("...");

to get it. If the classpath has your jar in it, it’ll be able to find the resources in there. If you don’t prepend a “/” to the path, then the path is assumed to be the “package path”, eg. if the class is “com.foo.Blah” then no slash will look under “com/foo/” for resources. If you want to package stuff up at the root level, prepend the “/” to the path.

It’s not rocket science!

Cas :slight_smile:

[quote]I’m a little dissapointed that I need to use “getResource” to get text files and pictures to be read from within the Jar file.
[/quote]
Why disappointed? That seems like a very odd thing to say. Kind of like, “I’m disappointed that I have to name my main method ‘main’.”
er, yeah, that just sucks…???.

I’m having all kinds of trouble with jars;

Edit: I fixed this… go ant…