开发者

How to check existence of a JAR file and a specific file inside JAR?

开发者 https://www.devze.com 2023-02-17 05:45 出处:网络
Assume the file name is in this URL: URL file = new URL(\"jar:file:/path/to/foo.jar!/META-INF/file.txt\");

Assume the file name is in this URL:

URL file = new URL("jar:file:/path/to/foo.jar!/META-INF/file.txt");

This operation leads to IOException if there is no foo.jar or there is no file.txt inside the existing jar:

import org.apache.commons.io.FileUtils;
FileUtils.copyURLToFile(file, /* some other file */);

Is it possible to validate file existence without exception c开发者_Python百科atching?


You can use the java.util.jar.JarFile class, and use the getJarEntry method to see if the file exists.

JarFile jar = new JarFile("foo.jar");
JarEntry entry = jar.getJarEntry("META-INF/file.txt");
if (entry != null) {
    // META-INF/file.txt exists in foo.jar
}


You can examine the CLASSPATH and search for the jar yourself. Then you can inspect the jar yourself. I guess it will not be faster than your way, nor will it be more easy to understand, so why do you want to avoid the Exception?

I don't see something wrong in using the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消