listing resources in jar files...

I’m seeing blah talking about putting games on webstart everywhere. As of now I’ve put all images and soundfiles in their own directories…

My noobish question is, how do I get a list of the things that are in a jar file… so I can use the list to go grab resources…

I could probably find this on my own… but as I have very limited time, I want to focus on developing the game… so I thought there must be a lot of users having done this, that can point me in the right direction without having to look anything up… :-)…

thanx for your time!

jar -tvf my_jar_file_here

or just rename the file to have a .ZIP extension and use your favorite archiving tool.

or, if you’re trying to do it programmatically, have a look at the ClassPath code on JGF:

http://javagamesfactory.org/views/view-sourcesnippet?id=1

…which finds all the classes in a JAR, amongst other things. IIRC the bits that read from a JAR file and find out whats in it are fairly obvious. They use standard API’s - you could just search the javadocs instead.

or if you use winrar, just add the .jar to the lsit of extensions it knwos for zip files :slight_smile:

Do you really want to encourage people to break the concept of the executable .JAR file, on purpose? :slight_smile:

I would suggest not changing the extension association… enough archiving programs break that on their own. Just open your archiving utility and drag and drop the JAR file on to it. That should do the trick. On windows you might also be able to right-click and choose “Open with…”

A double-click on an executable JAR file should launch the Java program it contains.

Exactly what I do all the time. XP “remembers” your open-with history, and makes a custom sub-menu automatically offering you the previously used open-withs for that particular filetype as a shortcut.

No problem.

Well… I was looking at doing it programatically… so I’ll take a look at blah’s link… thanx!

Actually… I might just produce a listing from the stuff unpacked… and put the listing in the jar file as well. Might be the easiest way to go… (admittedly not very flexible… but it’ll suit my needs…)

thanx for suggestion’s tho… :slight_smile:

…good idea though with open with… I’ll start using that myself too :slight_smile:

Shouldnt that be ClassLocator.java instead of ClassLocater.java? :slight_smile:

Aramaz,

The java.util.zip package can help you with this. Here’s a very simple example:

    ZipFile zipFile = new ZipFile(new File("c:/myjar.jar"));
    Enumeration entries = zipFile.entries();
    while (entries.hasMoreElements())
    {
        System.err.println(entries.nextElement().toString());
    }

Hope this helps.

Oh, thanx Mr EEK… that was exactly what I was after!

Is it possible to get a list of files in a directory inside a .jar ?

Let say that I have the following in a .jar:
/shaders/phong.cg
/shaders/bump.cg

To load a specific shader I do:
getClass().getResourceAsStream("/shaders/phong.cg");

But If I want to know the avilable resources in lets say “/shader/”.

If I try get ClassLoader.getResources(“shaders”)
I just get the url to the different .jars containg a shader folder and not
a list of the files in the folder

If I do a System.getProperty( “java.class.path” ) I get jars assigned to the classpath.
In the best of worls my problmes would end here… but the .jars assigned in
a webstart .jnlp file doesn’t seem to be on the path.

In the .jnlp I do:

How to solve the problem ?
// Tomas ???

Tomas, it’s not a nice solution but you could include a file in your JAR that contains the list of shaders.

Then:

getClass().getResourceAsStream("/shaders/list.txt");

and go through the file reading the listed shader file names, and read each using another getResourceAsStream call.

Not too elegant, but it would work (as long as you keep the list file up-to-date ;))

Seems like a horrible work around :wink: There must be a better way …

// Tomas

By the complete silence on this topic I guess that there is no solution… :frowning:

Could you not convert the URL to a File, then use the File’s abilities to list stuff?

Pseudocode:


URL t = getClass().getResource("/shaders");
File f = new File(t.getAbsolutePath());
if (f.isDirectory()) {
  for (int i = 0; i < f.numberOfChildren;i++) {
    Sysout(f.getChild(i);
  }
}

?

DP

That’d probably only work if the URL actually was a physical file reference. The URL type id dependent on the classloader in place.

Kev

I thought the programmatic way(s) had already been referenced above and there was no need to comment further?

Yes but this problem still remains, how do I get my hands on the .jars when using webstart.

See my earlier post:

// Tomas