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
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