Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file inside jar to outside the jar?

I want to copy a file from a jar. The file that I am copying is going to be copied outside the working directory. I have done some tests and all methods I try end up with 0 byte files.

EDIT: I want the copying of the file to be done via a program, not manually.

like image 696
jtl999 Avatar asked Apr 25 '12 01:04

jtl999


People also ask

How do I access files inside a jar?

You can use getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead. Show activity on this post. You could read the contents of a JAR file using the JarFile class. Show activity on this post.

How do you reference a file in a jar?

If you add the resources directory in the jar file (so it is under the /resources folder in the jar, and if /src/main is in your build path in eclipse, then you should be able to reference your file as: getClass(). getResource("/resources/filename. txt");


1 Answers

First of all I want to say that some answers posted before are entirely correct, but I want to give mine, since sometimes we can't use open source libraries under the GPL, or because we are too lazy to download the jar XD or what ever your reason is here is a standalone solution.

The function below copy the resource beside the Jar file:

  /**      * Export a resource embedded into a Jar file to the local file path.      *      * @param resourceName ie.: "/SmartLibrary.dll"      * @return The path to the exported resource      * @throws Exception      */     static public String ExportResource(String resourceName) throws Exception {         InputStream stream = null;         OutputStream resStreamOut = null;         String jarFolder;         try {             stream = ExecutingClass.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree             if(stream == null) {                 throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");             }              int readBytes;             byte[] buffer = new byte[4096];             jarFolder = new File(ExecutingClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');             resStreamOut = new FileOutputStream(jarFolder + resourceName);             while ((readBytes = stream.read(buffer)) > 0) {                 resStreamOut.write(buffer, 0, readBytes);             }         } catch (Exception ex) {             throw ex;         } finally {             stream.close();             resStreamOut.close();         }          return jarFolder + resourceName;     } 

Just change ExecutingClass to the name of your class, and call it like this:

String fullPath = ExportResource("/myresource.ext"); 

Edit for Java 7+ (for your convenience)

As answered by GOXR3PLUS and noted by Andy Thomas you can achieve this with:

Files.copy( InputStream in, Path target, CopyOption... options) 

See GOXR3PLUS answer for more details

like image 103
Ordiel Avatar answered Sep 22 '22 09:09

Ordiel