Reputation: 11
I am trying to access a file using java.io.File from the running jar using index. I mean, like creating the array of the files in that folder and then get a file by its index.
Upvotes: 1
Views: 141
Reputation: 4937
jar tf file.jar
command can be used to list down contents of jar file.
An example of jar tf command utitlity:
> jar tf demo-1.0.0.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
...
org/springframework/boot/loader/jar/
org/springframework/boot/loader/jar/JarURL
The same result can be obtained programmatically also, by using the java.jar.Jarfile class
Here is an example of programmatic lookup of contents of jar file:
// File name: JarLs.java
// This program lists down all the contents of a .jar file
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.Enumeration;
public class JarLs {
public static void main(String[] args) throws Exception {
JarFile jarFile = new JarFile("D:/test/demo-1.0.0.jar");
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
System.out.println(jarEntries.nextElement().getName());
}
}
}
Output:
> javac JarLs.java
> java JarLs
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
org/springframework/boot/loader/archive/
org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
org/springframework/boot/loader/WarLauncher.class
...
...
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/com/
...
...
BOOT-INF/classes/com/demo/controller/WebTrafficController.class
BOOT-INF/classes/com/demo/SpringBootWebApplication.class
More information:
https://docs.oracle.com/javase/tutorial/deployment/jar/view.html
http://www.devx.com/tips/java/reading-contents-of-a-jar-file-using-java.-170629013043.html
Upvotes: 1