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 getResource() to obtain a URL for a file on the classpath, or getResourceAsStream() to get an InputStream instead. Show activity on this post. You could read the contents of a JAR file using the JarFile class.
To find the . jar files that contain a class, you can use the FindClass.sh script. First go to a UNIX installation of Sterling Platform/MCF. If the FindClass.sh script already exists it should be in your $YFS_HOME directory or your $YFS_HOME/lib directory.
Right-click anywhere in the source window and select "Select In Projects". The class will show highlighted in the jar where it came from.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With