How to save a resource from an executable jar file to local drive?
I wonder if there is a way to extract a resource file packaged in an executable jar file and save it to a local drive, so when a user down开发者_Python百科loads my jar file and double clicks on it, it will first save one file from the resource to his C: drive, then run my program.
Just use ClassLoader.getResourceAsStream() to get an InputStream
to the contents of that resource, and write it to (say) the temp directory. In your main()
method just do this before you do execute the main part of your program.
You would need to write that logic into your program. Executing a JAR
file is just going to run the main class. If the first thing the main
method in that class does is to copy the file to a location on disk, it seems like this would meet your requirements.
Here is the Sun tutorial on copying a file in Java.
精彩评论