Extracting a file from the currently running JAR t

2019-02-08 17:40发布

问题:

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.

回答1:

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.



回答2:

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


回答3:

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)?