[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getting the name of a jar from inside the jar



I made my own JarFile class for that purpose. The public inner class MyJarEntry can access its outer Jar file.
 
Please let me know if you have any questions.
 
Alan
 
 
 
MyJarFile Class
 
public class MyJarFile extends JarFile {
 
      public MyJarFile( String str ) throws IOException {
        super( str );
      }
 
      public Enumeration entries() {
         final Enumeration enum = super.entries();
         return new Enumeration() {
             public boolean hasMoreElements() {
              return enum.hasMoreElements();
             }
             public Object nextElement() {
                 ZipEntry ze = (ZipEntry)enum.nextElement();
                  return new MyJarEntry(ze, MyJarFile.this );
             }
         };
      }
 
      public class MyJarEntry extends JarEntry {
        private JarFile parentJarFile;
         MyJarEntry(ZipEntry ze, JarFile pJarFile) {
             super(ze);
                parentJarFile = pJarFile;
         }
        public JarFile getParentJarFile() {
            return parentJarFile;
        }
      }
}
 
 
Test Program
 
public class Untitled1 {
 
  public static void main(String[] args) {
    try {
      MyJarFile aJar = new MyJarFile( "c:\\temp\\ant-iplanet.jar");
      Enumeration enu = aJar.entries();
      while ( enu.hasMoreElements() ) {
        MyJarFile.MyJarEntry myJFE = ( MyJarFile.MyJarEntry )enu.nextElement();
        System.out.println( myJFE.getParentJarFile().getName() );
      }
    }
    catch ( IOException e ) {
        e.printStackTrace();
    }
  }
}
----- Original Message -----
Sent: Thursday, March 28, 2002 3:23 PM
Subject: getting the name of a jar from inside the jar

I have a jar. This jar has code in it similar to this:
 
========================================================
JarFile studyJar = new JarFile(studyID+"."+studyVersion+".jar");
Enumeration enum = studyJar.entries();
========================================================
 
 
studyJar is a JarFile representation of the jar this code is executing in. I get studyID and studyVersion from a text file located inside the jar.
 
I need to add the functionality where the jar can be renamed by end users.
 
Is it possible for code inside of a jar to get the name of the jar it resides in? I need its name to instantiate the JarFile above.
 
Thanks,
 
-Jason
 
************************************************************************************
Jason R. Kretzer                               
Software Engineer                           
Opinion One
Email:  jason@opone.com
Phone:  (513) 361-2771
Website:  http://alia.iwarp.com
 
"Make visible what, without you, might perhaps never have been seen."
--Robert Bresson
************************************************************************************