File path to inside of .jar
I need a bit of help :(whew): 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
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: 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:(happy):?
P.S. Could it be the ! after BioCraft_Installer.jar?
Thanx,
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!
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).
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
Re: File path to inside of .jar