Results 1 to 3 of 3
  1. #1
    Frggr is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Java applet - List files within a jar file.

    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:

    Java Code:
    /*
     * 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.SEVERE, null, ex);
            }
           
        }
    }
    The Jar file looks like this:
    Java Code:
    something.jar
    -something
    --Repo.class
    -Audio
    --<empty>
    And the output would look like this:
    Java Code:
    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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    getClass().getResources() method because that returns a http:\\ link
    An idea: Read from that link and pass the bytes to the JarFile class. To do that you need to Create a class that extends the File class and pass that to the JarFile's constructor. Override the methods that the JarFile calls to get the bytes of the jar file and return the bytes that you read from the server

    Another idea to insert a file with the full listing of the contents that the applet could read.

  3. #3
    Frggr is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    An idea: Read from that link and pass the bytes to the JarFile class. To do that you need to Create a class that extends the File class and pass that to the JarFile's constructor. Override the methods that the JarFile calls to get the bytes of the jar file and return the bytes that you read from the server
    I'll look into this, sounds like it should do exactly what I'm aiming for. Thanks for the help.
    =D

Similar Threads

  1. list files on the java server
    By cmp_sft in forum Networking
    Replies: 1
    Last Post: 01-02-2011, 12:19 AM
  2. can you tell how transfer a List eg. created as...between files jsp/java
    By lse123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 21
    Last Post: 02-10-2010, 08:52 AM
  3. how to list certain files in a given directory in java
    By corpusluteum in forum New To Java
    Replies: 6
    Last Post: 09-24-2008, 12:19 AM
  4. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:15 AM
  5. Replies: 20
    Last Post: 12-17-2007, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •