PDA

View Full Version : Java - List filess in a jar file



Frggr
August 6th, 2011, 01:19 AM
I need to find out how to list files in a jar file from a class within that jar file.

My current method is something like this:



/*
* NewJApplet.java
*
* Created on 5-Aug-2011, 5:54:13 PM
*/
package something;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author amccann
*/
public class Repo extends javax.swing.JApplet {

/** Initializes the applet NewJApplet */
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {

public void run() {
initComponents();
initJarFile();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
// Variables declaration - do not modify
// End of variables declaration

private void initJarFile() {
URL url = this.getClass().getClassLoader().getResource("Audio");

int startIndex = url.getPath().indexOf("://") == -1 ? url.getPath().indexOf(":/") + 1 : url.getPath().indexOf("://") + 1;
try {
JarFile jar = new JarFile(url.getPath().substring(startIndex, url.getPath().indexOf("!")));
for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
System.out.println(e.nextElement().getName());
}
} catch (IOException ex) {
Logger.getLogger(Repo.class.getName()).log(Level.S EVERE, null, ex);
}

}
}



The Jar file looks like this:


something.jar
-something
--Repo.class
-Audio
--<empty>


And the output would look like this:


Audio
something
something/Repo.class


The problem I run into is when I try to deploy this onto a website.

The I can't load the JarFile using the getClass().getResources() method because that returns a http:\\ link that JarFile doesn't know how to interpret.

I've spent hours trying to figure out how to get a list of files but the only thing I can come up with is to use an external program to list them and then pass them in as a variable. (Which is not ideal and limits the functionality that I need.)

Please help!

-Thanks

Reiger
August 6th, 2011, 03:37 AM
You can get the resource as InputStream through the classloader. I expect JarFile can deal with InputStream objects as well.

VernonA
August 11th, 2011, 06:40 PM
From the info you supply I think the specific answer to your question is to check for the http in the URL and then use a URLConnection to get to it. Eg, use something like:

if (url.getProtocol().equals("jar")
// do what you do now
else if (url.getProtocol().equals("http")
{
JarURLConnection juc = (JarURLConnection) url.openConnection();
// now you can get JarEntry like you are used to
}
I have never tried this myself so I don't know how far you will get with it.

However, before you go too far I would advise you to look at using JNLP to deploy your app, as I have found WebStart to be a great way of delivering RIAs to users. Even applets can be distributed this way, side-stepping the problems you are encountering. You probably won't need to change your code at all, but you will need to sign your Jar. This is well-explained on various websites. There are some minor issues you will encounter, but overall WebStart is pretty mature and works well.