How to copy files out of the currently running jar

后端 未结 3 2166
鱼传尺愫
鱼传尺愫 2020-12-01 22:38

I have a .jar that has two .dll files that it is dependent on. I would like to know if there is any way for me to copy these files from within the .jar to a users temp folde

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-01 23:23

    Use a class loader that is able to locate resources in this JAR file. Either you can use the class loader of a class as Peter Lawrey suggested, or you can also create a URLClassLoader with the URL to that JAR.

    Once you have that class loader you can retrieve a byte input stream with ClassLoader.getResourceAsStream. On the other hand you just create a FileOutputStream for the file you want to create.

    The last step then is to copy all bytes from the input stream to the output stream, as you already did in your code example.

提交回复
热议问题