Results 1 to 5 of 5
  1. #1
    roemba is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Talking File path to inside of .jar

    I need a bit of help with a path to a file inside a package inside my .jar. I'm trying to access the file from a class inside another package in the same class. The goal is finally to get a path which I can later use to copy the file from the package to somewhere outside the package (I have the copying part, I just can't figure out the method to get the path). Here is what I get when I run the function combinelocal
    Java Code:
     private static File combinelocal(String apath)
    { 
            FileTransfers hallo = new FileTransfers();
            URL url = hallo.getClass().getResource("/files/");
        
            String locacom = url.getPath();
            locacom = locacom.replace("%20", " ");
            locacom = locacom + apath; 
        
        
            File combined = new File(finaldes); 
            System.err.println(combined);
            return combined;
    }
    This function is designed to get the path to where the .jar is located and then gets the path of the resource(package) folder 'files' which contains the items I want to copy. The variable apath will be combined with that initial path so I can always call the function when I need to get a file out of that package. When I run this in NetBeans it runs totally fine without errors while if I run it outside of NetBeans I get an error that it can't find the file. The function then (when the error occurs) puts out the path:
    Java Code:
    /Users/randomuser/NetBeansProjects/BioCraft Installer/dist/BioCraft_Installer.jar!/files/test.lua
    My question is, is it possible to get a file path to the inside of a .jar package and so yes, how do I get to the files then? Or is there something I oversee in my process of getting this right?

    P.S. Could it be the ! after BioCraft_Installer.jar?

    Thanx,

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: File path to inside of .jar

    Jar files are essentially zip files, so ordinary path navigation won't work unless your OS supports browsing zip files in that way. However, java does support this itself since this is how applications are loaded at runtime anyway. There is a collection of tools for doing exactly this, one place to start is JarFile (Java Platform SE 6)

    That said, there are many tools and packages that do a lot of the grunt work for you. Apache Ant Apache Ant - Welcome can do all kinds of things regarding packaging and repackaging jars, and if you want something with a softer learning curve, you could always just play around with the built in jar tools and see where that gets you. There are many ways to extract, combine, and rebundle jar files if you want!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: File path to inside of .jar

    If you know the file inside the jar (which you appear to) use getResourceAsStream(), which will give you the InputStream.
    Use that directly to read bytes into a byte buffer and write that buffer to the OutputStream directly.
    Don't worry about paths (beyond the resource location).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: File path to inside of .jar

    A small nitpick: jar entries are not files. There's only the one file -- the jar file. The contents are entries.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: File path to inside of .jar

    *thrrrrrrp*
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. File Path
    By novice in forum New To Java
    Replies: 12
    Last Post: 08-04-2011, 03:25 PM
  2. file path
    By billq in forum New To Java
    Replies: 2
    Last Post: 04-11-2010, 05:47 PM
  3. ant with a jar file in path to run
    By javastuden in forum Advanced Java
    Replies: 0
    Last Post: 02-22-2010, 05:59 AM
  4. file path
    By ras_pari in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2009, 11:25 AM
  5. file path
    By Arsench in forum New To Java
    Replies: 7
    Last Post: 10-30-2008, 05:39 AM

Tags for this Thread

Posting Permissions

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