java - How to copy files out of the currently running jar -


i have .jar has 2 .dll files dependent on. know if there way me copy these files within .jar users temp folder @ runtime. here current code have (edited 1 .dll load reduce question size):

public string tempdir = system.getproperty("java.io.tmpdir"); public string workingdir = dllinstall.class.getprotectiondomain().getcodesource().getlocation().getpath();  public boolean installdll() throws unsupportedencodingexception {  try {              string decodedpath = urldecoder.decode(workingdir, "utf-8");              inputstream fileinstream = null;              outputstream fileoutstream = null;               file filein = new file(decodedpath + "\\loadatruntime.dll");              file fileout = new file(tempdir + "loadatruntime.dll");               fileinstream = new fileinputstream(filein);              fileoutstream = new fileoutputstream(fileout);               byte[] bufferjni = new byte[8192000013370000];              int lengthfilein;               while ((lengthfilein = fileinstream.read(bufferjni)) > 0) {                 fileoutstream.write(bufferjni, 0, lengthfilein);              }              //close steams         } catch (ioexception e) {       e.printstacktrace();              return false;         } catch (unsupportedencodingexception e) {              system.out.println(e);               return false;         } 

my main problem getting .dll files out of jar @ runtime. way retrieve path within .jar helpful.

thanks in advance.

since dlls bundeled inside jar file try acasses them resources using classloader#getresourceasstream , write them binary files want on hard drive.

here sample code:

inputstream ddlstream = <someclassinsidethesamejar>.class     .getclassloader().getresourceasstream("some/pack/age/somelib.dll");  try (fileoutputstream fos = new fileoutputstream("somelib.dll");){     byte[] buf = new byte[2048];     int r;     while(-1 != (r = ddlstream.read(buf))) {         fos.write(buf, 0, r);     } } 

the code above extract dll located in package some.pack.age current working directory.


Comments