java -jar jarname does not unzip all resource files
I am creating an executable jar with maven and have added the following to my pom.xml under build section
<build>
.....
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/snmp</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
.....
</build>
And I use the maven-shade-plugin to build jar with dependenc开发者_高级运维ies.
When I run the command after the build
java -jar jarName
It does not unzip all the files under src/main/snmp directory, for some reason it always unzips one file (the same file) every time. But if I do
jar -xf jarName
this unzips everything correctly.
Any other thing that I need to for using resources from a executable jar?
Java -jar isn't supposed to extract anything, it's supposed to run whatever main method you've declared in your manifest.
What do you mean by "use resources from an executable jar"? If you mean access resources on the classpath from within the application, if you're doing it right, it should work fine-but there's no extraction.
The command java -jar jarName.jar
tries to execute the class, found in the jar, whose name is given by the Main-Class
attribute in the manifest. In no way is it supposed to unzip the file. The command jar xf jarname.jar
, on the other hand, is supposed to do exactly that.
If running the program with java -jar
is unpacking something from the archive, then it's because the program found in the archive is designed to do so.
You definitely don't want to be extracting a JAR to run it. It's supposed to be self-contained. Put your resources in src/main/resources, and they'll be built into your jar at the root directory. Then use Class.getResource() or Class.getResourceAsStream() to get it as a classpath resource.
Edit: Based on your comments, the first thing I think of is to go ahead and pack the files into your JAR, then at runtime, use File.createTempFile() to write them out to the default temporary directory, then give that directory to the third-party library to use. You can use File.deleteOnExit() to clean up afterward.
精彩评论