I need to open a file which is inside a maven jar package. It a model configuration for a framework I use and a constructor of a library class requires to pass a object of type File. I can get a path to a configuration file using class loader without any problems. But -- unfortunately -- File can not read a file inside jar. So I get java.io.FileNotFoundException. Now I looking for a solution for this problem. My plan is to decompress the model configuration file and place it in a temporary directory. However, before star开发者_开发问答ting coding, I would like to learn if there is any other solution for such a problem like mine.
UPDATE: I need to read a file in runtime.
If you're doing it from a maven build, unpack the jar resource to a file using
dependency:unpack-dependencies
(if the jar is one of the project's maven dependenies)<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack-dependencies</id> <phase>generate-resources</phase> <goals> <goal>unpack-dependencies</goal> </goals> <configuration> <includeGroupIds>the.groupId</includeGroupIds> <includeArtifactIds>the.artifactId</includeArtifactIds> <includes>**/path/to/your/resource.txt</includes> <outputDirectory>where/do/you/want/it</outputDirectory> </configuration> </execution> </executions> </plugin>
or use
dependency:unpack
(if the jar is no dependency, but still available as a maven artifact)<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack</id> <phase>generate-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>the.groupid</groupId> <artifactId>the.artifactid</artifactId> <version>the.version</version> <type>jar</type> <outputDirectory>where/do/you/want/it</outputDirectory> <includes>**/path/to/your/resource.txt</includes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>
I think you should use JarInputStream, traversing through JAR entries one by one, until you find what you need. Then just read()
content of the found JarEntry
.
精彩评论