JAR files in launched app's JAR (navigating URLConnection/JarURLConnection?)

Discussion in 'Java' started by Karsten Wutzke, Jun 27, 2007.

  1. Hi all!

    I have an application bundled like this (one single JAR containing
    library JAR's):

    +- rootpackage
    | +- Main.class
    | +...
    +- images
    +- lib
    | +- bcel-5.2.jar
    | +- forms-1.1.0.jar
    | +- ...
    +- ...

    When starting up, I create a URL like this:


    //the JAR inside the JAR
    String strRelativePath = "lib/bcel-5.2.jar";

    //determines the class and protocol for the connection, prepend with
    URL url = this.getClass().getResource("/" + strRelativePath);


    The URL string of url is "jar:file:/C:/dev/bew/myapp.jar!/lib/
    bcel-5.2.jar" which is correct.

    Now I'm trying to extract the *sub* (lib) JAR's executing:


    //this is the myapp.jar JarFile instance, that is the ENCLOSING,
    running app's JAR file
    //and not the one requested ("lib/bcel-5.2.jar")!
    JarURLConnection juc = (JarURLConnection)url.openConnection(); //
    returns connection to *enclosing* JAR


    I had expected to get a direct connection to the JAR file inside the
    JAR, however the currently running app's JAR file is the connection
    target. This is somewhat unexpected! When launching the app directly
    from the class files directory with the *same* lib hierarchy and the
    classpath containing an entry to the root dir ".", the connection is
    made directly to the JAR wanted... (here FileURLConnection with string
    "file:/C:/dev/bew/lib/bcel-5.2.jar"") which is the desired behavior...

    Now I have to perform a number of steps to extract the JAR manually


    JarFile jarEnclosing = juc.getJarFile();

    JarEntry je = jarEnclosing.getJarEntry(strRelativePath);

    //get input stream of JAR entry and transform to byte array
    byte[] uncompressedBytes = new byte[(int)je.getSize()];

    DataInputStream dis = new
    dis.readFully(uncompressedBytes, 0, uncompressedBytes.length);



    I now have the plain byte[] of the JAR inside the app JAR, this is ok
    so far, but I was wondering if there is a way via JarURLConnection to
    get to the sub lib JarFile instance *directly* by dereferencing/
    navigating the URL instance somehow so I don't have to go via the
    enclosing JAR...?

    Karsten Wutzke, Jun 27, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.