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 downloads 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.
+1
A:
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.
danben
2010-02-28 18:27:59
Is that copyTo() method referenced only in Java7/OpenJDK ? And will it copy from a resource contained *within* a .jar file ?
Brian Agnew
2010-02-28 18:40:45
+2
A:
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.
Brian Agnew
2010-02-28 18:32:08