Extracting a file from the currently running JAR through code

Konstantin picture Konstantin · Jul 13, 2012 · Viewed 14.7k times · Source

Are there any built-in methods I can use to allow users to extract a file from the currently running JAR and save it on their disk?

Thanks in advance.

Answer

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