using java.util.jar.JarFile

Of course Java can read zip files / jars. It’s pretty straightforward:

import java.util.jar.JarFile;
import java.util.Enumeration;
//...
    public void print( String arg ) throws Exception {
        this.print( new JarFile( arg ) );
    }

    public void print( JarFile jarFile ) {
        this.print( jarFile.entries()  );
    }

    public void print( Enumeration entries ) {
        while ( entries.hasMoreElements() ) {
            System.out.println( entries.nextElement() );
        }
    }

program, compile and execute thyself!

I wrote a little test program called Jarout.java, which is a “self-compiling” program:

% ./Jarout.java ~/.m2/repository/org/springframework/spring/2.5.5/spring-2.5.5.jar

This works (in reasonable environments) because the first 4 lines are a shell script protected by a block comment:

/*2222222 2>/dev/null
javac Jarout.java && java Jarout ${*}
exit ${?}
*/

import java.util.jar.JarFile;
import java.util.Enumeration;
//...

The first line keeps the shell quiet and the exit keeps the rest of the program from being interpreted. It’s a silly trick, but useful sometimes. I also use it for C and C++. The gcc is so fast that it gives traditional interpreted languages a run for their money!

Even though it is a silly trick, it allows you to use any compiled language like a scripting language…

😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: