Extracting a file from the currently running JAR through code

こ雲淡風輕ζ 提交于 2019-11-30 12:44:25
Dave Newton

Use getResourceAsStream (docs), you can do whatever you want with it after that.

For a two-liner you could use one of the Commons IO copy methods.

Renato
File file = new File("newname.ext");
if (!file.exists()) {
     InputStream link = (getClass().getResourceAsStream("/path/resources/filename.ext"));
     Files.copy(link, file.getAbsoluteFile().toPath());
}
GuruKulki

I am not sure whether you will get know from which jar your class is getting executed but you can try this to extract resources from jar : How to write a Java program which can extract a JAR file and store its data in specified directory (location)?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!