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 catching?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.